| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1020 人关注过本帖
标题:【编程中。。快来救救小女!】模仿书上的bridge编法函数调用不了啊啊啊
只看楼主 加入收藏
xixifans
Rank: 1
等 级:新手上路
帖 子:118
专家分:2
注 册:2012-11-15
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:19 
【编程中。。快来救救小女!】模仿书上的bridge编法函数调用不了啊啊啊
求问如下图中
程序代码:
class fund
{
public:
fund(){};
virtual double get_netvalue() const=0;
virtual double get_stockprice() const=0;
virtual ~fund(){};
virtual fund* clone() const=0;//使用复制构造函数(克隆)


private:
};

class motherfund: public fund//不知道为什么把这个继承改成public openendfund就不对了。。?这是为什么呢?
{
public:
    motherfund(double netvalue_) : netvalue(netvalue_) {};
    virtual ~motherfund(){};

    virtual fund* clone() const//function clone
    {return new motherfund(*this);
    }

    virtual double get_netvalue() const
    {return netvalue;}
    virtual double get_stockprice() const
    {return netvalue;//不知道这样改了会不会bug....
    }

private:
    double netvalue;
};


//子基金类定义
class umbrellafund : public fund//不知道为什么把这个继承改成public openendfund就不对了。。?这是为什么呢?
{
public:
    umbrellafund(double netvalue_,double stockprice_) : netvalue(netvalue_),stockprice(stockprice_) {};
    virtual ~umbrellafund(){};

    virtual fund* clone() const //function clone
    {return new umbrellafund(*this);
    }
   
    virtual double get_netvalue() const
    {return netvalue;}
    virtual double get_stockprice() const
    {return stockprice;}
private:
    double netvalue;
    double stockprice;
};

以上是本来的类定义


以下是我模仿老师要求的建立过渡类
程序代码:
class fundbridge
{public:
fundbridge(const fundbridge& bridge)
{
    fundpointer=bridge.fundpointer->clone();
}

fundbridge(const fund& innerfund)
{fundpointer=innerfund.clone();
}

~fundbridge()
    {delete fundpointer;
}

fundbridge& operator=(const fundbridge& bridge)
{if(this !=&bridge)
{delete fundpointer;
fundpointer=bridge.fundpointer->clone();
}
return *this;
}


private:
    fund* fundpointer;
};

class LOFfund
{public:
LOFfund(const fundbridge& thefundbridge_,int shares_):thefundbridge(thefundbridge_),shares(shares_)
{}

double getshares() const
{return shares;}

private:
    fundbridge thefundbridge;
    int shares;
};

自我感觉建立的时候应该是没有什么错的吧。

不过在我想在我定义的函数中调用原来的成员函数getvalue()的时候
它就出bug了
程序代码:
double MonteCarlo_n2(const LOFfund& theloffund,
                         double Expiry, 
                         double Vol,
                         double r,
                         unsigned long NumberOfPaths)
{//首先从fund的派生来获得其现价
   
    double present_price=theloffund.getvalue();//就是这里啊!为什么不能访问呢?这里为什么不能访问呢?


    double variance = Vol*Vol*Expiry;
    double rootVariance = sqrt(variance);
    double itoCorrection = -0.5*variance;

    double movedSpot = present_price*exp(r*Expiry +itoCorrection);
    double thisfutureprice;
    double runningSum=0;

    for (unsigned long i=0; i < NumberOfPaths; i++)
    {
        double thisGaussian = GetOneGaussianByBoxMuller2();
        thisfutureprice = movedSpot*exp( rootVariance*thisGaussian);
        runningSum += thisfutureprice;
    }

    double mean = runningSum / NumberOfPaths;
    mean *= exp(-r*Expiry);
        double future_price= mean;
    return future_price;
}


跪求解决啊啊啊啊!谢大神啊
搜索更多相关主题的帖子: 模仿 编程 bridge 
2012-11-23 19:51
w527705090
Rank: 10Rank: 10Rank: 10
等 级:贵宾
威 望:11
帖 子:441
专家分:1882
注 册:2011-6-28
收藏
得分:3 
帮顶先~~~不懂啊 。。。。

有心者,千方百计;无心者,千难万难。
2012-11-23 20:13
xixifans
Rank: 1
等 级:新手上路
帖 子:118
专家分:2
注 册:2012-11-15
收藏
得分:0 
回复 2楼 w527705090
我企图把这个问题解决掉,在fund.h里强行插入一个函数getnetvalue();再不用const类型,
我是这样改的
程序代码:
class fundbridge
{public:
fundbridge(const fundbridge& bridge)
{
    fundpointer=bridge.fundpointer->clone();
}

fundbridge(const fund& innerfund)
{fundpointer=innerfund.clone();
}

~fundbridge()
    {delete fundpointer;
}
//这里强插getnetvalue了
double getnetvalue()
{return fundpointer->get_netvalue();
}
fundbridge& operator=(const fundbridge& bridge)
{if(this !=&bridge)
{delete fundpointer;
fundpointer=bridge.fundpointer->clone();
}
return *this;
}


private:
    fund* fundpointer;
};

class LOFfund
{public:
LOFfund(const fundbridge& thefundbridge_,int shares_):thefundbridge(thefundbridge_),shares(shares_)
{}

int getshares() const
{return shares;}

//与上面的强插同步
double getnetvalue()
{return thefundbridge.getnetvalue();}



private:
    fundbridge thefundbridge;
    int shares;
};
#endif
定义改了以后的函数
程序代码:
//为了让下面这个函数能跑,我吧LOFfund前面的const给去掉了
double MonteCarlo_n2(LOFfund& theloffund,
                         double Expiry, 
                         double Vol,
                         double r,
                         unsigned long NumberOfPaths)
{//首先从fund的派生来获得其现价
   
    double present_price=theloffund.getnetvalue();//这样改了它没有报错了



然后就出现了更大的问题。。我的另外一个函数文件根本不能识别。。
程序代码:
double LOFarbitrage2(const LOFfund& motherfund_,const fund& umbrellafundA_,const fund& umbrellafundB_)
{double Pr=0.015;

 double Rr=0.005;

 double Br=0.0005;


 //获得当时的基金股价和母基金净值
 double ma=umbrellafundA_.get_stockprice();

 double mb=umbrellafundB_.get_stockprice();

 double na=motherfund_.get_netvalue();

 int shares=motherfund_.getshares();



 //获得利用蒙特卡洛方法模拟后的价格
 double vol_=0.1;

 double r_=0.1;

 double Numberpaths_=1000;


 double Ma=MonteCarlo_s(umbrellafundA_,3/365,vol_,r_,Numberpaths_);

 double Mb=MonteCarlo_s(umbrellafundB_,3/365,vol_,r_,Numberpaths_);


 //获得利用蒙特卡洛方法模拟后的净值
 double Na=MonteCarlo_n(motherfund_,1/365,vol_,r_,Numberpaths_);



 double condition1=(Ma+Mb)*(1-Br)-2*na*(1+Pr);

 //[1]    (Ma+Mb)*x*(1-br)> 2x*Nm*(1+Pr):T+3可以卖出
 double condition2=(ma+mb)*(1-Br)-2*Na*(1+Rr);

 //[2]    (Ma+Mb)*x*(1-br)< 2x*Nm*(1+Rr):T+1可以卖出

if (condition1>0)
    return condition1*shares_;
else if (condition2<0)
    return (-1)*condition2*shares_;
else
    return 0;
}

运行一下全报错。说这个程序的LOFfund,fund
全部都是未定义的类型。。。我用头文件包括的了呀!搞什么啊
程序代码:
1>LOFarbitrage2.cpp(3): error C4430: 缺少类型说明符 - 假定为 int。注意: C++ 不支持默认 int
1>LOFarbitrage2.cpp(3): error C2143: 语法错误 : 缺少“,”(在“&”的前面)
1>LOFarbitrage2.cpp(9): error C2065: “umbrellafundA_”: 未声明的标识符
1>LOFarbitrage2.cpp(9): error C2228: “.get_stockprice”的左边必须有类/结构/联合
1>          类型是“'unknown-type'1>LOFarbitrage2.cpp(10): error C2065: “umbrellafundB_”: 未声明的标识符
1>LOFarbitrage2.cpp(10): error C2228: “.get_stockprice”的左边必须有类/结构/联合
1>          类型是“'unknown-type'1>LOFarbitrage2.cpp(11): error C2065: “motherfund_”: 未声明的标识符
1>LOFarbitrage2.cpp(11): error C2228: “.get_netvalue”的左边必须有类/结构/联合
1>          类型是“'unknown-type'1>LOFarbitrage2.cpp(12): error C2065: “motherfund_”: 未声明的标识符
1>LOFarbitrage2.cpp(12): error C2228: “.getshares”的左边必须有类/结构/联合
1>          类型是“'unknown-type'1>LOFarbitrage2.cpp(20): error C2065: “umbrellafundA_”: 未声明的标识符



有没有大神。求告诉我它好好的为什么不认识我的类了!!


看那没有形象的发型和憔悴的脸就知道这是一只和C++恋爱的宅女。yes,I am!
2012-11-23 20:28
w527705090
Rank: 10Rank: 10Rank: 10
等 级:贵宾
威 望:11
帖 子:441
专家分:1882
注 册:2011-6-28
收藏
得分:0 
我以前也遇到过这种让人头疼的未声明的标识符。。。。。。。。
可能是有些细节的地方没有注意到,等大神来解答吧 。。。

有心者,千方百计;无心者,千难万难。
2012-11-23 20:46
xixifans
Rank: 1
等 级:新手上路
帖 子:118
专家分:2
注 册:2012-11-15
收藏
得分:0 
回复 4楼 w527705090
我很恶俗的把我所有CPP里的东西搬到了.h文件里,然后把CPP删掉了。于是就好了。。哈哈

不过我后来定义的地方我只有把函数函数的const去掉才能运行。一直不知道为什么啊

看那没有形象的发型和憔悴的脸就知道这是一只和C++恋爱的宅女。yes,I am!
2012-11-23 20:53
w527705090
Rank: 10Rank: 10Rank: 10
等 级:贵宾
威 望:11
帖 子:441
专家分:1882
注 册:2011-6-28
收藏
得分:0 
看来是实践出真知啊。。。楼主威武啊~~~
const问题去问下这个版主:lz1091914999
他好像在线。。。我不懂

有心者,千方百计;无心者,千难万难。
2012-11-23 20:59
xixifans
Rank: 1
等 级:新手上路
帖 子:118
专家分:2
注 册:2012-11-15
收藏
得分:0 
回复 6楼 w527705090
好。。俺去骚扰一下。先贴出来好了
程序代码:
//为了让下面这个函数能跑,我吧LOFfund前面的const给去掉了
double MonteCarlo_n2(LOFfund& theloffund,
                         double Expiry, 
                         double Vol,
                         double r,
                         unsigned long NumberOfPaths)
{//首先从fund的派生来获得其现价
   
    double present_price=theloffund.getnetvalue();//这样改了它没有报错了


    double variance = Vol*Vol*Expiry;
    double rootVariance = sqrt(variance);
    double itoCorrection = -0.5*variance;

    double movedSpot = present_price*exp(r*Expiry +itoCorrection);
    double thisfutureprice;
    double runningSum=0;

    for (unsigned long i=0; i < NumberOfPaths; i++)
    {
        double thisGaussian = GetOneGaussianByBoxMuller2();
        thisfutureprice = movedSpot*exp( rootVariance*thisGaussian);
        runningSum += thisfutureprice;
    }

    double mean = runningSum / NumberOfPaths;
    mean *= exp(-r*Expiry);
        double future_price= mean;
    return future_price;
}

这里是一个地方我必须把const去掉才能跑

还有一个也是
程序代码:
;double LOFarbitrage2(LOFfund& motherfund_,const fund& umbrellafundA_,const fund& umbrellafundB_)//去掉了const类型
{double Pr=0.015;
double Rr=0.005;
double Br=0.0005;

//获得当时的基金股价和母基金净值
double ma=umbrellafundA_.get_stockprice();
double mb=umbrellafundB_.get_stockprice();
double na=motherfund_.getnetvalue();
int shares=motherfund_.getshares();


//获得利用蒙特卡洛方法模拟后的价格
double vol_=0.1;
double r_=0.1;
double Numberpaths_=1000;

double Ma=MonteCarlo_s(umbrellafundA_,(double)3/365,vol_,r_,Numberpaths_);
double Mb=MonteCarlo_s(umbrellafundB_,(double)3/365,vol_,r_,Numberpaths_);

//获得利用蒙特卡洛方法模拟后的净值
double Na=MonteCarlo_n2(motherfund_,(double)1/365,vol_,r_,Numberpaths_);


double condition1=(Ma+Mb)*(1-Br)-2*na*(1+Pr);
//[1]    (Ma+Mb)*x*(1-br)> 2x*Nm*(1+Pr):T+3可以卖出
double condition2=(ma+mb)*(1-Br)-2*Na*(1+Rr);
//[2]    (Ma+Mb)*x*(1-br)< 2x*Nm*(1+Rr):T+1可以卖出

if (condition1>0)
    return condition1*shares;
else if (condition2<0)
    return (-1)*condition2*shares;
else
    return 0;
}
为什么不能用const呢???




看那没有形象的发型和憔悴的脸就知道这是一只和C++恋爱的宅女。yes,I am!
2012-11-23 21:08
xixifans
Rank: 1
等 级:新手上路
帖 子:118
专家分:2
注 册:2012-11-15
收藏
得分:0 
我可以说我大概乱捣鼓出来了嘛!!!!!!!!!把一些与LOFfund有关的地方的函数和数据成员声明为const之后就可以const了!

不过我还是不是很明白这个具体的道理。。有些模模糊糊的。。

看那没有形象的发型和憔悴的脸就知道这是一只和C++恋爱的宅女。yes,I am!
2012-11-23 21:18
TonyDeng
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:贵宾
威 望:304
帖 子:25859
专家分:48889
注 册:2011-6-22
收藏
得分:3 
我都没明白你整个程序的构思是什么

授人以渔,不授人以鱼。
2012-11-24 00:44
xixifans
Rank: 1
等 级:新手上路
帖 子:118
专家分:2
注 册:2012-11-15
收藏
得分:0 
回复 9楼 TonyDeng
明儿睡个懒觉起来发个逻辑总结帖去

看那没有形象的发型和憔悴的脸就知道这是一只和C++恋爱的宅女。yes,I am!
2012-11-24 00:58
快速回复:【编程中。。快来救救小女!】模仿书上的bridge编法函数调用不了啊啊啊 ...
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.019836 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved