2014-04-04 49 views
1

請幫我解決這個問題。我試圖解決這個問題兩個小時。 這是我的代碼。找不到默認構造函數來初始化cpp中的成員

class deviceC { 

private: 
    deviceA devA; 
    deviceB devB; 
    wayPoint destination,current; 

public: 
    deviceC(wayPoint destination1){ 
     destination=destination1; 
     devA=deviceA(); 
     devB=deviceB(); 
    } 
}; 

這是錯誤:

cannot find default constructor to initialize member 'deviceC::destination' in function deviseC::destination(wayPoint)

+0

請發表更多代碼。你在哪裏試圖創建一個'deviceC'類型的對象? – MrDuk

+0

我們可以看到wayPoint的構造函數嗎? –

+0

泰克斯每個人。在添加一個像@songyuanyao所說的初始化列表後,它正在工作。 –

回答

3

你需要在構造函數初始化列表,因爲成員destinationcurrentwayPoint類型不具有默認構造函數。

class deviceC { 
public: 
    deviceC(wayPoint destination1) : destination(destination1) { 
     devA=deviceA(); 
     devB=deviceB(); 
    } 
}; 

和國際海事組織,你不需要初始化的devAdevB構造只是默認的構造函數中,他們只是叫他們operator=稱爲默認構造函數後。這是我的建議:

class deviceC { 
private: 
    deviceA devA; 
    deviceB devB; 
    wayPoint destination, current; 
public: 
    deviceC(const wayPoint& destination1, const wayPoint& current1) : destination(destination1), current(current1) {} 
}; 
+0

它現在正在工作。我也必須初始化其他變量「current」。謝謝.. –

+0

@kasunbdn我編輯了我的答案,以提供更多建議,希望它有所幫助。 – songyuanyao

+0

是的,這是非常有幫助的。我在java中編寫代碼,現在正在嘗試使用C++。但無法得到「你不需要在構造函數中使用默認構造函數初始化devA和devB」,所以當我們初始化devA和B時? –

0

錯過了一個括號。

class deviceC{ 

    private : deviceA devA; 
        deviceB devB; 
        wayPoint destination,current; 

    public: deviceC(wayPoint destination1){ 
      destination=destination1; 
      devA=deviceA(); 
      devB=deviceB(); 
    } // <-- here 
}; 
+0

相比很抱歉。當我在這裏複製代碼時,我是個錯誤。 thakns。它正在工作 –

相關問題