2016-11-06 204 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

對不起。你能解釋清楚嗎? –

相關問題