C++ 正确实例化双重继承对象?
我试图使用双重继承,将一个更复杂的对象声明为两个简单对象的总和。但是,当我尝试按如下方式进行操作时:C++ 正确实例化双重继承对象?,c++,inheritance,C++,Inheritance,我试图使用双重继承,将一个更复杂的对象声明为两个简单对象的总和。但是,当我尝试按如下方式进行操作时: class common_cfullr { public: double core; double thr; double thrres; common_cfullr(double Core, double Thr, double Thrres){ core=Core; thr=Thr; thrres=Thrres; }; ~common_cful
class common_cfullr
{
public:
double core;
double thr;
double thrres;
common_cfullr(double Core, double Thr, double Thrres){
core=Core;
thr=Thr;
thrres=Thrres;
};
~common_cfullr(){};
common_cfullr() :
core(0.0),
thr(0.0),
thrres(0.0)
{}
};
class common_cfull
{
public:
int nelec;
int ms2;
common_cfull (int Nelec,
int Ms2){
nelec =Nelec;
ms2 =Ms2;
};
~common_cfull(){};
common_cfull() :
nelec(0),
ms2(0){}
};
///Structure inheritance
class common :
public common_cfullr,
public common_cfull
{public:
common(){};
~common(){};
};
int main(int argc, char** argv)
{
common cmn();
cmn.nelec=0;
return 0;
}
我从编译器那里得到一个错误。为此,如何正确使用双重继承
任何建议都将不胜感激。(基于Neik Kirk的评论)更改通用cmn()代码>至普通cmn代码>使您的代码编译时不会出错
使用clang++
进行编译时,编译器发出的注释也会告诉您:
t.cc:48:13: warning: empty parentheses interpreted as a function declaration
[-Wvexing-parse]
common cmn();
^~
t.cc:48:13: note: remove parentheses to declare a variable
common cmn();
^~
如果你有一个错误,它会很高兴你分享它说的<代码>公共cmn()代码>这是最烦人的解析(谷歌it)。您必须使用通用cmn代码>它实际上不是最麻烦的解析,但它是相关的。这是一个常见的误解。非常感谢您的帮助!