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