正确实例化双重继承对象?

时间:2014-08-20 22:53:33

标签: 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_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;
}

我从编译器收到错误。为此目的,我如何才能正确使用双重继承?

任何消化都将非常感激。

1 个答案:

答案 0 :(得分:2)

(基于Neik Kirk的评论。)将common cmn();更改为common cmn;会使您的代码编译而不会出错。

使用clang++进行编译时,编译器发出的 note 也会告诉您:

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();
            ^~