2
我有一個簡單的代碼混亂的局面:C++ 11 POD結構初始化錯誤
struct Item {
size_t span{};
};
int main() {
Item item{1}; // error is here
return 0;
}
在編寫這個我下面的錯誤:
test.cpp: In function ‘int main()’:
test.cpp:8:13: error: no matching function for call to ‘Item::Item(<brace-enclosed initializer list>)’
Item i{1};
^
test.cpp:8:13: note: candidates are:
test.cpp:3:8: note: constexpr Item::Item()
struct Item {
^
test.cpp:3:8: note: candidate expects 0 arguments, 1 provided
test.cpp:3:8: note: constexpr Item::Item(const Item&)
test.cpp:3:8: note: no known conversion for argument 1 from ‘int’ to ‘const Item&’
test.cpp:3:8: note: constexpr Item::Item(Item&&)
test.cpp:3:8: note: no known conversion for argument 1 from ‘int’ to ‘Item&&’
爲什麼g++
試圖找到一個ctor
爲initializer list
在這種情況下,而不是簡單的C風格的結構對象創建?
如果我從size_t span{}
刪除{}
它編譯成功。
如果我行更改爲size_t span = 0
這也恰好所以它似乎是因爲C++ 11所存在問題的聲明一些初始化。
'Item'不是C++ 11中的一個聚集類型(它是C++ 14中的一個聚合) –
@PiotrSkotnicki謝謝,剛剛發現爲什麼我確實認爲它會好起來的 - 閱讀一本關於c + +14,這就是爲什麼我認爲它也適用於C++ 11。 –
自1985年以來C++還沒有結構。關鍵字'struct'聲明瞭一個_class_。 –