8

I'm using Visual Studios 2013 and I keep getting this error yet I don't understand why.

class CLI{
    string commands[2] = {"create", "login"};
public:
    void addCommand(), start(), getCommand(string);
};

The error:

error C2536: 'CLI::CLI::commands': cannot specify explicit initializer for arrays
11
  • 1
    It should work fine. Out of curiosity, does this one work for you? Commented May 27, 2014 at 22:52
  • 2
    I won't ask what this is supposed to be: void addCommand(), start(), getCommand(string);, since it isn't related to your question. Commented May 27, 2014 at 22:58
  • 1
    You cannot initialize class members in this way before C++11. You need to do it in a constructor instead. Commented May 27, 2014 at 23:00
  • 2
    @chris: I said before C++11. Commented May 27, 2014 at 23:03
  • 1
    @TobiasBrandt, It's getting better. They are actually putting in a big effort to get caught up in that regard. I'm guessing VS2015 will be (nearly?) finished with C++14, which is much better than they did for C++11 at least. VS2014 should have most of it, too. Commented May 27, 2014 at 23:24

2 Answers 2

15

Visual Studio 2013 is not completely C++11 compliant, so, like Tobias Brandt said, you'll need to use a constructor to initialize those members.

Braced init lists are a C++11 feature.

Sign up to request clarification or add additional context in comments.

Comments

2

I don't think that in-class member initializers are implemented in VC2013. Instead, initialize the array in a constructor. For example:

class CLI{
    string commands[2];
public:
    CLI() : commands {"create", "login"}
    {}
};

2 Comments

I get the same error involving "cannot specify explicit initializer for arrays" when using the constructor
It's not supported then. You'll have to initialize it in the constructor then. e.g "CLI(){commands[0] = "create";commands[1] = "login";}"

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.