struct SS {int a; int s;};
int main ()
{
vector<SS> v;
v.push_back(SS{1, 2});
}
Run Code Online (Sandbox Code Playgroud)
可以编译代码而不会出现任何错误.但是,当在类中初始化struct时,我得到了编译错误.有人能解释一下吗?
struct SS {int a = 0; int s = 2;};
Run Code Online (Sandbox Code Playgroud)
错误:
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: …Run Code Online (Sandbox Code Playgroud)