決定我有下面的代碼:奇怪的編譯器
enum nums {
a
};
class cls {
public:
cls(nums);
};
void function()
{
cls(a);
}
當我嘗試用gcc編譯它,我得到以下錯誤:
test.cpp: In function ‘void function()’:
test.cpp:12:10: error: no matching function for call to ‘cls::cls()’
test.cpp:12:10: note: candidates are:
test.cpp:7:3: note: cls::cls(nums)
test.cpp:7:3: note: candidate expects 1 argument, 0 provided
test.cpp:5:7: note: cls::cls(const cls&)
test.cpp:5:7: note: candidate expects 1 argument, 0 provided
make: *** [test] Error 1
如果我這個替換功能:
void function()
{
cls name(a);
}
然後一切正常。如果我使用帶兩個參數的構造函數,它也可以工作。如果我將「顯式」添加到構造函數,它不起作用。
我得到的gcc是以某種方式解析這個定義類型爲「cls」的名稱爲「a」的變量,但我不熟悉定義變量的這種語法。在我看來,這是一個定義類型cls的匿名臨時變量的語句,傳遞「a」是參數。
編譯gcc 4.6.3。
任何見解?
感謝, Shachar
Err ...你怎麼想'cls(a);'* should * do ?! –
在問題中如此說明 - 創建一個類型爲「Cls」的臨時對象。 –