Jeffrey Cordero
Jeffrey Cordero

Reputation: 792

VSC13 - cannot specify explicit initializer for arrays / for STRINGS

I understand that Microsoft Visual Studios Community 2013 has a problem with the initialization of arrays, but how do I work around this specifically for strings? And please try to explain the answer well, I am still quite new to this.

class a{
public:
    string words[3] = {"cake","pie","steak"};
};

Upvotes: 0

Views: 981

Answers (2)

cdmh
cdmh

Reputation: 3344

As you wrote it won't compile because you can't initialize a non-static array within the definition. This works though:

#include <array>
class a{
public:
    a() : words({"cake","pie","steak"})
    {
    }

    std::array<std::string, 3> words;
};

Upvotes: 2

Igor Tandetnik
Igor Tandetnik

Reputation: 52471

Are you looking for something like this?

class a{
public:
  string words[3];

  a::a() {
    words[0] = "cake";
    words[1] = "pie";
    words[2] = "steak";
  }
};

Upvotes: 1

Related Questions