返回信息流我定义了这样的一个虚基类
class Vehicle
{
private:
string m_Kind;
double m_Weight;
double m_Speed;
public:
Vehicle()
{
m_Kind = " ";
m_Weight = 0.0;
m_Speed = 0.0;
}
Vehicle(const string& str, double w, double s)
{
m_Kind = str;
m_Weight = w;
m_Speed = s;
}
virtual double GetWeight() const = 0;
virtual double GetSpeed() const = 0;
virtual Vehicle* Copy() const = 0;
virtual ~Vehicle() = 0;
}
从这个类中派生了一个类
class AutoMoblie:public Vehicle
{
private:
int m_Wheels;
public:
// AutoMobile():m_Wheels(0),Vehicle() {}
AutoMoblie(const string& str, double w, double s,int wheel)
:m_Wheels (wheel),Vehicle(str,w,s) {}
virtual double GetWeight() const;
virtual double GetSpeed() const;
virtual Vehicle* Copy() const;
};
在VC下编译不会有错,但是将注释掉的那条语句 (// AutoMobile():m_Wheels(0),Vehicle() {})恢复,就会报错
e:\vc++\c++\surrogateclass\surrogateclass.h(38) : error C2327: 'AutoMoblie::m_Wheels' : member from enclosing class is not a type name, static, or enumerator
e:\vc++\c++\surrogateclass\surrogateclass.h(38) : error C2065: 'm_Wheels' : undeclared identifier
e:\vc++\c++\surrogateclass\surrogateclass.h(38) : error C2057: expected constant expression
e:\vc++\c++\surrogateclass\surrogateclass.h(38) : error C2656: 'AutoMobile' : function not allowed as a bit field
e:\vc++\c++\surrogateclass\surrogateclass.h(38) : error C2086: 'Vehicle' : redefinition
e:\vc++\c++\surrogateclass\surrogateclass.h(38) : error C2143: syntax error : missing ';' before '{'
e:\vc++\c++\surrogateclass\surrogateclass.h(38) : error C2334: unexpected token(s) preceding '{'; skipping apparent function body
请大家帮我看看是怎么回事?
这是一条镜像帖。来源:北邮人论坛 / cpp / #6891同步于 2008/5/15
该镜像源已超过 30 天没有更新,可能在源站已被删除。
CPP机器人发帖
在派生类中调用虚基类的构造函数
kangkai810
2008/5/15镜像同步6 回复
订阅后,新回复会通过你的通知中心匿名送达。
6 条回复
缺了分号,轮子类没定义,函数原型和定义有冲突,我暂时只看到这些。
【 在 kangkai810 (kangkai810) 的大作中提到: 】
: 我定义了这样的一个虚基类
: class Vehicle
: {
: ...................
class AutoMoblie:public Vehicle
// AutoMobile():m_Wheels(0),Vehicle() {}
类名与构造函数名都不一致