注册 登录
编程论坛 C++教室

【小宅女编了一下午啊】可是为什么我定义的类不能识别

xixifans 发布于 2012-11-20 16:37, 1370 次点击
亲们。我下午用我自己能理解的方法编了编我想要编的程序,无奈我写好了以后报了一大堆错,我认真看了看大部分就是说我定义的类它觉得不对,或者是识别别不了,求懂得大牛牛小牛牛帮我看看吧!!猜测这个对懂得人应该是一个很小的问题吧。求解救!!爱你们!我最后一点点分也散在这里吧。。不够去别的帖子回复吧

我把我写的.h&.cpp贴出来

类定义头文件
程序代码:
#include <iostream>
using namespace std;

class fund
{
public:
    fund(){};


private:
};

class motherfund: public fund
{
public:
    motherfund(double netvalue_) : netvalue(netvalue_) {};

private:
    double netvalue;
};

class umbrellafund : public fund
{
public:
    umbrellafund(double netvalue_,double stockprice_) : netvalue(netvalue_),stockprice(stockprice_) {};
private:
    double netvalue;
    double stockprice;
};

操作函数头文件
程序代码:
#include <iostream>
#include "classfund.h"//为什么我include了这个头文件它识别不到呢???
using namespace std;

double LOFarbitrage(const motherfund& motherfund_,const umbrellafund& umbrellafundA_,const umbrellafund& umbrellafundB_,int shares_)

操作函数具体说明.CPP
程序代码:
#include <iostream>
#include "classfund.h"//为什么我include了这个头文件它识别不到呢???
using namespace std;
double LOFarbitrage(const motherfund& motherfund_,const umbrellafund& umbrellafundA_,const umbrellafund& umbrellafundB_,int shares_)
{double Pr=0.015;

 double Rr=0.005;

 double Br=0.0005;


 double condition1=umbrellafundA_.stockprice+umbrellafundB_.stockprice)*(1-Br)-(2*motherfund_.netvalue*(1+Pr);

 //[1]    (Ma+Mb)*x*(1-br)> 2x*Nm*(1+Pr):
double conditon2=umbrellafundA_.stockprice+umbrellafundB_.stockprice)*(1-Br)-(2*motherfund_.netvalue*(1+Rr);

 //[2]    (Ma+Mb)*x*(1-br)< 2x*Nm*(1+Rr):

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


我的主程序
程序代码:

int main()
{double mothernetvalue;//输入需要的五个值,具体需要的输入函数没写
double umbrellavalueA,umbrellapriceA;
double umbrellavalueB,umbrellapriceB;
int theshares;//用多少份来进行套利

cout<<"输入母基金净值"<<endl;
cin>>mothernetvalue;

cout<<"输入子基金A净值"<<endl;
cin>>umbrellavalueA;

cout<<"输入子基金A市价"<<endl;
cin>>umbrellapriceA;

cout<<"输入子基金B净值"<<endl;
cin>>umbrellavalueB;

cout<<"输入子基金B市价"<<endl;
cin>>umbrellapriceB;

cout<<"每次套利多少份"<<endl;
cin>>theshares;

motherfund the_motherfund(mothernetvalue);
umbrellafund umbrellaA(umbrellavalueA,umbrellapriceA);//建立需要的五个变量
umbrellafund umbrellaB(umbrellavalueB,umbrellapriceB);


double thepayoff=LOFarbitrage(the_motherfund,umbrellaA,umbrellaB,theshares)//代入这个函数计算有没有套利,套利出来是多少,为了简便没有的时候设为0

if (thepayoff==0)
    cout<<"此时无套利机会"<<endl;
if (thepayoff!=0)
    cout<<"此时有套利机会,可以套利"<<thepayoff<<endl;

}




25 回复
#2
寒风中的细雨2012-11-20 17:08
//bccn.c
#include<stdio.h>
#include<stdlib.h>

这样子注明文件名  好理解           
你这上面到底有几个文件?
#3
xixifans2012-11-20 17:21
回复 2楼 寒风中的细雨
四个文件,两个CPP两个H文件。第一个是我定义的类,报错就是说我类重定义或者找不到类。另外两个CPP都要include它才可以。PS如果我用尖括号,它识别不了我include的文件
#4
寒风中的细雨2012-11-20 17:36
回复 3楼 xixifans
类重定义

把类的成员函数提炼出来  实现单独用一个cpp文件


#5
xixifans2012-11-20 17:38
回复 4楼 寒风中的细雨
我还木有设成员函数。。。我好像知道了。因为要加个
ifndef
define
endif..的东东。。。thx。。我再去改改其他的错
#6
xixifans2012-11-20 18:31
经过我的一番修改。我现在的错误主要是“不能实例化抽象类”,这是什么意思呢?
类定义头文件
程序代码:
#ifndef FUND_H
#define FUND_H

class fund
{
public:
fund(){};
virtual double get_netvalue() const=0;
virtual double get_stockprice() const=0;


private:
};

class motherfund: public fund
{
public:
    motherfund(double netvalue_) : netvalue(netvalue_) {};
    virtual double get_netvalue()
    {return netvalue;}
    virtual double get_stockprice()
    {}

private:
    double netvalue;
};

class umbrellafund : public fund
{
public:
    umbrellafund(double netvalue_,double stockprice_) : netvalue(netvalue_),stockprice(stockprice_) {};
    virtual double get_netvalue()
    {return netvalue;}
    virtual double get_stockprice()
    {return stockprice;}
private:
    double netvalue;
    double stockprice;
};
#endif
是我这里定义的有问题嘛?

函数定义头文件
程序代码:
#include <iostream>
#include "fund.h"
using namespace std;

double LOFarbitrage(const motherfund& motherfund_,const umbrellafund& umbrellafundA_,const umbrellafund& umbrellafundB_,int shares_)
函数CPP
程序代码:
#include <iostream>
#include "fund.h"//为什么我include了这个头文件它识别不到呢???
using namespace std;
double LOFarbitrage(const motherfund& motherfund_,const umbrellafund& umbrellafundA_,const umbrellafund& umbrellafundB_,int shares_)
{double Pr=0.015;

 double Rr=0.005;

 double Br=0.0005;


 double condition1=(umbrellafundA_.get_stockprice()+umbrellafundB_.get_stockprice())*(1-Br)-2*motherfund_.get_netvalue()*(1+Pr);

 //[1]    (Ma+Mb)*x*(1-br)> 2x*Nm*(1+Pr):
double condition2=(umbrellafundA_.get_stockprice()+umbrellafundB_.get_stockprice())*(1-Br)-2*motherfund_.get_netvalue()*(1+Rr);

 //[2]    (Ma+Mb)*x*(1-br)< 2x*Nm*(1+Rr):

if (condition1>0)
    return condition1*shares_;
else if (condition2<0)
    return (-1)*condition2*shares_;
else
    return 0;
}
主函数(主函数中
motherfund the_motherfund(mothernetvalue);
umbrellafund umbrellaA(umbrellavalueA,umbrellapriceA);//建立需要的五个变量
umbrellafund umbrellaB(umbrellavalueB,umbrellapriceB);
会报错:“不能实例化抽象类”
程序代码:

#include "fund.h"
#include "LOFarbitrage.h"
#include <iostream>
;using namespace std;


int main()
{double mothernetvalue;//输入需要的五个值,具体需要的输入函数没写
double umbrellavalueA,umbrellapriceA;
double umbrellavalueB,umbrellapriceB;
int theshares;//用多少份来进行套利

cout<<"输入母基金净值"<<endl;
cin>>mothernetvalue;

cout<<"输入子基金A净值"<<endl;
cin>>umbrellavalueA;

cout<<"输入子基金A市价"<<endl;
cin>>umbrellapriceA;

cout<<"输入子基金B净值"<<endl;
cin>>umbrellavalueB;

cout<<"输入子基金B市价"<<endl;
cin>>umbrellapriceB;

cout<<"每次套利多少份"<<endl;
cin>>theshares;

motherfund the_motherfund(mothernetvalue);
umbrellafund umbrellaA(umbrellavalueA,umbrellapriceA);//建立需要的五个变量
umbrellafund umbrellaB(umbrellavalueB,umbrellapriceB);


double thepayoff=LOFarbitrage(the_motherfund,umbrellaA,umbrellaB,theshares);//代入这个函数计算有没有套利,套利出来是多少,为了简便没有的时候设为0

if (thepayoff==0)
    cout<<"此时无套利机会"<<endl;

if (thepayoff!=0)
    cout<<"此时有套利机会,可以套利"<<thepayoff<<endl;

}






 
#7
寒风中的细雨2012-11-20 18:35
学到抽象类了?
#8
寒风中的细雨2012-11-20 18:38
virtual double get_netvalue() const=0;

 virtual double get_stockprice() const=0;

    virtual double get_netvalue();
     virtual double get_stockprice();


注意下形式  不是相同的函数
继承了抽象类  如果想实例化  必须实现抽象类的方法(全部)
#9
lz10919149992012-11-20 18:44
什么是识别不到?是预处理器找不到文件?如果是这样的话请看看头文件和cpp文件是不是在同一目录下。类重定义?如果一个cpp文件里只包含这个头文件一次的话是不会出现的。

你还得把LOFarbitrage声明为motherfund和umbrellafund的友元函数,直接引用私有变量是不行的,或着分别为每个类写一个setter和getter。

如果简单的问题呢,用过程式的方法来处理就好了,没必要非得建立类,还建立继承关系,别以为这样就觉得自己很专业。
#10
xixifans2012-11-20 18:46
回复 8楼 寒风中的细雨
我把所有的virtual和const去掉之后就可以生成了。

我不是很明白你说的意思,如果想要保留虚函数的话,我应该怎么做实例化啊??可不可以给小解释一下子

txthx
#11
lz10919149992012-11-20 18:51
不知道楼主要实现什么功能?看起来也不是很高深啊?何必写出这样的代码来,那两个类无非就是对一两个字段的封装,写个struct就足够了。居然还用到了继承,抽象类?
#12
xixifans2012-11-20 18:51
回复 9楼 lz1091914999
前两个问题已经解决了!
现在的问题是类的定义里面因为写了virtual,主函数那里不能实例化啊。去掉就可以了。

P.S.老师非让用类啊= =牛肉满面。。还要用clone啥的。。我还没有研究出来。。

#13
lz10919149992012-11-20 18:56
回复 10楼 xixifans
get_netvalue和get_stockprice在基类里的函数签名是有const的,而你实现的时候却没有。
#14
xixifans2012-11-20 18:58
回复 11楼 lz1091914999
大体来说是想实现fund类中netvalue和stockprice不相等且满足某些条件时的套利,得到一个关于(netvalue和stockprice)的收益。

而fund类会派生出来各种具体的形式,比如我写的这个就是motherfund和两个umbrellafund来满足某个条件。

我想用这个大思路来完成老师要求的【使用继承、多态、抽象类&clone函数等】做一个自定主题project的考试题目
#15
lz10919149992012-11-20 19:10
回复 14楼 xixifans
clone没猜错的话就是复制构造函数和operator=吧。
#16
xixifans2012-11-20 19:10
回复 13楼 lz1091914999
恩!像你这样说的改了就对了。。。

最后的一个小问题是这里
程序代码:
class motherfund: public fund
{
public:
    motherfund(double netvalue_) : netvalue(netvalue_) {};
    virtual double get_netvalue() const
    {return netvalue;}
    virtual double get_stockprice() const
    {return netvalue;//不知道这样改了会不会bug....
    }

private:
    double netvalue;
};
motherfund这里是没有stockprice这个成员的。。但是如果我没有返回值的话就会报错,但是也不能声明为纯虚函数了。我就强制的给它get_stockprice()中return了netvalue,其实这是不对的。。。
#17
xixifans2012-11-20 19:13
回复 15楼 lz1091914999
好像是那个!!!!!!!!!书上写的叫做virtual copy constructor

哇哇哇。。可不可以求告诉我它是干嘛用的。。应该怎么使用在函数里面呀
#18
lz10919149992012-11-20 19:17
回复 16楼 xixifans
呵呵,其实你并没有调用这个函数啊,返回什么值都可以了。如果不怕代码蛋疼拿去通过考试是没问题的。

其实这两个类之间是没有共性的,也就是说fund这个类是没用的,除了增加复杂性以外。
#19
xixifans2012-11-20 19:30
回复 18楼 lz1091914999
恩。貌似是的。。我这一系列的类,我最基础的想法是所有的fund都会有netvalue,有的还有stockprice,有的还会增加其他指标,就可以某一些增加一些成员。这是不是建立派生类的正确思路啊?我不是很确定。。

容我从考试的角度问一句这里咋加入复制构造函数吧

#20
xixifans2012-11-20 19:31
回复 18楼 lz1091914999
对了。。这个帖子只有5分。。你可以随便回下我另外的散分贴,我从那些帖子给你结分
#21
lz10919149992012-11-20 19:32
virtual copy constructor?
在基类中加个virutal fund* clone() const = 0;然后每个类以协变的方式实现就行了,比如:
class fund {
public:
    ...
    virtual fund* clone() const = 0;
    ...
}

class motherfund : public fund {
public:
    ...
    virtual motherfund* clone() const = 0
    {
        return new motherfund(*this);
    }
    ...
};

...

int main()
{
    motherfund mf(...);
    motherfund* pmf2 = mf.clone();
    ...
    delete pmf2;
}
#22
宋立鹏2012-11-20 21:21
费那么大劲啊,private改为public不就行了吗,哈哈
#23
xixifans2012-11-21 00:23
回复 22楼 宋立鹏
这个想法吧= =哈哈哈。。
#24
Soaringup2012-11-21 00:29
回复 23楼 xixifans
程序猿的世界总是停留在黑夜
#25
xixifans2012-11-22 17:06
回复 24楼 Soaringup
是的。。已经几天都是一点睡觉了
#26
migen2012-11-28 13:46
.h是C语言里的。在VS编译器里不能编译。要改。不过我也是初学。所以不知道改些什么
1