2

I'm supposed to make a class which has a static vector as a variable.

   using namespace std;

class Foo
{
public:
    static vector<Player> PLAYERS;
};

In the .cpp file I got an undefined reference to the vector and I have been told I have to declare it first

vector<Player> Foo::PLAYERS;

My question is what does that declaration excactly do? Is it similar to the constructor of a class? If the vector wasn't static would I still have to declare it?

gsamaras
  • 71,951
  • 46
  • 188
  • 305
Default99
  • 47
  • 2
  • 2
    Possible duplicate of https://stackoverflow.com/questions/185844/how-to-initialize-private-static-members-in-c?noredirect=1&lq=1. See particularly Matt Curstis' answer https://stackoverflow.com/a/185863/5470596 – YSC May 25 '18 at 07:37
  • 6
    You've got the vocabulary mixed up. That vector is declared (but not defined) in the class. The statement you added is the definition. – Mat May 25 '18 at 07:37
  • you _need_ to define static membre when you don't want them to be default constructed – Tyker May 25 '18 at 07:39
  • 1
    @Tyker You also need to define them when you *do* want them default constructed, unless they're `constexpr` or a "compile-time only constant integral value." – Angew is no longer proud of SO May 25 '18 at 07:40

1 Answers1

0

In your header file, you declared the vector. That vector has the keyword static, thus every instance of the class Foo will share that one and only vector.

You need to define the vector as well. You cannot do it outside the class, since this will result in a compiler error. Thus, in your source file, you wrote:

vector<Player> Foo::PLAYERS;

which is the definition.

I wouldn't say that it is similar to a constructor, but rather like because there can be only one instance of your vector (thus you cannot have it in the constructor of Foo).

I like to think it like the case of a variable declared as extern in a header file, if that helps.

gsamaras
  • 71,951
  • 46
  • 188
  • 305
  • Thanks a lot that really helped out. One more thing: Do I have to use the :: operator every time I use the vector ( Foo::PLAYERS ... ) Or can I just use PLAYERS ... without a calling the class everytime? – Default99 May 25 '18 at 07:52
  • Good @Default99. You are not *calling* the class. This operator `::` is for scope resolution. If you are not taught what this operator does yet in your school, then be patient and trust your instructor. ;) PS: There is always the Internet, if you are that eager . . – gsamaras May 25 '18 at 07:55
  • Trusting the instructor is somewhat risky when it comes to C++, some do that a lot better than others. If in doubt, get a good C++ book you can really trust. – Baum mit Augen May 25 '18 at 07:59
  • 1
    @Default99 within the members of `Foo` the name `PLAYERS` refers to that vector, and using `Foo::PLAYERS` is redundant. – Caleth May 25 '18 at 08:26
  • I find the statement about the constructor misleading. When you define `vector Foo::PLAYERS;`, the compiler do add code (executed at some point before entering the `main` function) to call the default constructor of `vector`, initialising it to an empty vector. – Vincent Saulue-Laborde May 25 '18 at 08:51