-1

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?

6
  • 1
    Compiles clean for me g++ 5.1.0 (in full SO paranoid mode) Commented May 22, 2017 at 10:57
  • 1
    Can you please create a minimal reproducible example? Commented May 22, 2017 at 10:59
  • 3
    Which compiler? What compiler flags? Commented May 22, 2017 at 10:59
  • 1
    Are you sure you're compiling with the flag -std=c++11? Commented May 22, 2017 at 11:02
  • 5
    I think it's time we started closing so-called C++11 questions, in which the problem was the OP wasn't compiling C++11, as "no longer reproducible". Commented May 22, 2017 at 11:10

2 Answers 2

3

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:

-std=c++11
Sign up to request clarification or add additional context in comments.

Comments

0

That's correct c++11, but maybe you are not compiling in C++11 mode.

Many compiler still defaults to C++98, and you typically need to activate a command line switch (or an option in your IDE) to enable C++11 syntax.

I've added to your code a small main:

int main()
{
    Data d;
    B b(d);
}

... and it compiles clean both with gcc 5.x and clang 802 (xcode 8 version), provided I add on the command line:

-std=c++11

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.