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
  • 16,951
  • 12
  • 63
  • 111
Jeffrey Cordero
  • 602
  • 1
  • 7
  • 20

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,226
  • 2
  • 24
  • 40
  • 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
  • 48,636
  • 4
  • 54
  • 79