相关疑难解决方法(0)

c ++ 11 struct initialization compilation error

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)

c++ c++11

23
推荐指数
2
解决办法
1万
查看次数

标签 统计

c++ ×1

c++11 ×1