struct SS {int a; int s;};
int main()
{
vector<SS> v;
v.push_back(SS{1, 2});
}
该代码可以编译没有任何错误。但是,当结构在类中初始化时,我得到编译错误。任何人都可以解释吗?C++ 11结构初始化编译错误
struct SS {int a = 0; int s = 2;};
错误:
In function ‘int main()’:
error: no matching function for call to ‘SS::SS(<brace-enclosed initializer list>)’
v.push_back(SS{1, 2});
^
note: candidates are:
note: constexpr SS::SS()
struct SS {int a = 0; int s = 2;};
^
note: candidate expects 0 arguments, 2 provided
note: constexpr SS::SS(const SS&)
note: candidate expects 1 argument, 2 provided
note: constexpr SS::SS(SS&&)
note: candidate expects 1 argument, 2 provided
对,我正要回答相似。通过添加自定义构造函数SS(){}',可以看出没有C++ 11特性。 – hvd
C++ 14将不再使用类内初始化程序排除聚合;请参阅http:// www。open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3653.html – bames53
@ bames53谢谢,我不知道。这是一个好消息。我觉得这个特别的规则过分限制。 – juanchopanza