2014-10-17 155 views
0

我正在學習C++。我編寫了這個程序,但是編譯它時,程序顯示出了一些含糊不清的錯誤。 我不明白,如果我做一個沒有參數的對象,那麼它應該只調用默認的構造函數&程序應該運行沒有任何錯誤。下面是代碼:參數化構造函數的默認值是什麼意思?

#include<iostream> 
using namespace std; 
class room 
{ 
int length,width; 
public: 
room() 
{ 
    cout<<"Default constructor"; 
} 
room(int l=0) 
{ 
     cout<<"Constructor"; //the compiler is taking this also as default constructor 
} 
room(int l=0,int w=0) 
{ 
    cout<<"Constructor"; //the compiler is taking this also as default constructor on making other two as comment 
} 
}; 
int main() 
{ 
room r1; 
return 0; 
} 

我試圖對之類的編譯代碼塊,開發的C++ & GCC也驗證碼。

回答

3

room r1不明確,因爲有拖欠的所有參數的構造函數已經可以 作爲room()作爲默認的構造函數

§12.1

一類X默認構造函數是類的構造函數X表示 可以是而不帶參數。如果對於類X沒有用戶聲明的構造函數,則不帶參數的構造函數是 隱式聲明爲默認值(8.4)。

+0

謝謝先生您的迴應。我現在只想確認零是參數與深度值? – Harshal 2014-10-17 06:32:04

0

您有3個構造函數可以在不提供任何參數的情況下調用。 所以編譯器被這3個構造函數弄糊塗了。

相關問題