2016-11-06 130 views
0

我有一个类,它命名为A,并且一个类在其私有中另有3个类。初始化其他类构造函数内的类对象

class A{ 
    public: 
      A(); 
      A(int num); 
      A(C& mC, P& mP, M& mM, int num); 
    //there is a getter and setter for all member this only one example(please check are they right?) 
      M getM()const{return pM;} 
      void setM(M classM){ pM = classM ;} 
     private: 
      C& pC; 
      P& pP; 
      M& pM; 
      int digit= 0; 
     }; 

我这样做,在参数constucture:

A::A(C& mC, P& mP, M& mM, int num):pC(mc),pP(mP),pM(mM) 
{ 
// doing someting here 
} 

但我不能写默认和第一个参数建设浅析代码,当我写的东西编译器对我说的是:

error: uninitialized reference member in ‘class A&’ [-fpermissive] A::A(){

note: ‘A& A::pP’ should be initialized A& pP;

有些像这样,几个错误和笔记。

我该怎么办?我如何初始化默认和第一个参数结构中的类?

+0

仍然我找不到任何解决方案。 –

回答

1

类别A包含参考到其他对象。与指针不同,引用不能为空。为了使这项工作,您可能需要:

  • 使用指针而不是引用,并初始化为nullptr是没有有效的对象是在构造函数
  • 商店成员由值提供。这涉及原始参数的副本,而语义上的不同 - 可能不是您所需要的。
+0

对不起。你能解释清楚吗? –

相关问题