0

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"};
};
Filburt
  • 17,626
  • 12
  • 64
  • 115
Jeffrey Cordero
  • 752
  • 1
  • 7
  • 21

2 Answers2

2

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;
};
cdmh
  • 3,294
  • 2
  • 26
  • 41
  • Could you shortly explain what you mean by "can't initialize a non-static array within the definition"? Or just tell me what to google to understand it, thank you for the help though! – Jeffrey Cordero Jun 02 '15 at 21:41
1

Are you looking for something like this?

class a{
public:
  string words[3];

  a::a() {
    words[0] = "cake";
    words[1] = "pie";
    words[2] = "steak";
  }
};
Igor Tandetnik
  • 50,461
  • 4
  • 56
  • 85