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_。 –