简体   繁体   English

c++11 struct初始化编译错误

[英]c++11 struct initialization compilation error

struct SS {int a; int s;};

int main ()
{
   vector<SS> v;
   v.push_back(SS{1, 2});
}

The code can be compiled without any error.代码可以编译没有任何错误。 However, when the struct is initialized in class, I got compilation error.但是,在类中初始化结构时,出现编译错误。 Can anyone explain it?谁能解释一下?

struct SS {int a = 0; int s = 2;};

Error:错误:

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

In C++11, when you use non static data member initialization at the point of declaration like you do here:在 C++11 中,当你像这里一样在声明点使用非静态数据成员初始化时:

struct SS {int a = 0; int s = 2;};

you make the class a non-aggregate .你让班级成为非聚合 This means you can no longer initialize an instance like this:这意味着您不能再像这样初始化实例:

SS s{1,2};

To make this initialization syntax work for a non-aggregate, you would have to add a two-parameter constructor:要使此初始化语法适用于非聚合,您必须添加一个两个参数的构造函数:

struct SS 
{
  SS(int a, int s) : a(a), s(s) {}
  int a = 0; 
  int s = 2;
};

This restriction has been lifted in C++14.此限制已在 C++14 中取消。

Note that you may want to add a default constructor for the class.请注意,您可能希望为该类添加一个默认构造函数。 The presence of a user-provided constructor inhibits the compiler generated default one.用户提供的构造函数的存在禁止编译器生成默认构造函数。

See related reading here .请参阅此处的相关阅读。

Use of a default member initializer renders the class/struct a non-aggregate:使用默认成员初始值设定项将类/结构呈现为非聚合:

§ 8.5.1 Aggregates § 8.5.1 聚合

An aggregate is an array or a class (Clause 9) with no user-provided constructors (12.1), no brace-or-equal-initializers for non-static data members (9.2), no private or protected non-static data members (Clause 11), no base classes (Clause 10), and no virtual functions (10.3).聚合是一个数组或类(第 9 条),没有用户提供的构造函数(12.1),没有用于非静态数据成员的大括号或等号初始化器(9.2),没有私有或受保护的非静态数据成员(第 11 条),没有基类(第 10 条),也没有虚函数(10.3)。

Semantics differ for aggregates and non-aggregates:聚合和非聚合的语义不同:

Aggregates (eg, arrays and structs):聚合(例如,数组和结构):

Initialize members/elements beginning-to-end.

Non-aggregates:非聚合:

Invoke a constructor.

v.push_back(SS{1, 2}); // Error, it tries to call SS constructor

Which means you need a constructor now:这意味着您现在需要一个构造函数:

struct SS 
{
  SS(int a, int s) : a(a), s(s) 
  {
  }
  int a = 0; 
  int s = 2;
};

I had the same problem.我有同样的问题。 In my case, I had two struct s that both had a few constructors, including copy constructors, inheriting from an abstract parent.在我的例子中,我有两个struct ,它们都有一些构造函数,包括复制构造函数,从抽象父继承。

When the advice above didn't help, I finally realized I needed to remove explicit specifier from the copy constructors and that removed the error.当上面的建议没有帮助时,我终于意识到我需要从复制构造函数中删除explicit说明符并删除错误。

Thought I would share in case another poor soul spends as long finding this mistake as I just did.我想我会分享,以防另一个可怜的灵魂像我刚刚做的那样花很长时间发现这个错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM