pepero pepero -4 years ago 186
C++ Question

error: too many initializers for a struct

I'm trying some code with c++11,

struct Data {};

struct B {
B(Data data) : m_data{data} {}
Data m_data{};

it complains
error: too many initializers for 'Data'

what is wrong?

[UPDATE] thanks guys, there is something wrong with my toolchain configuration.

Answer Source

You get that error string when you do not enable c++11 mode or later in older GCC compilers (that defaults to c++03).

main.cpp:4:31: error: too many initializers for 'Data'
B(Data data) : m_data{data} {}

See it here. Although newer versions of GCC will give you more helpful diagnostics to enable c++11 mode.

So, just add to your compiler invocation:

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download