0

Possible Duplicate:
Access issue regarding static variable

I'm having what seems to be a really trivial problem but I can't seem to work out what the cause is.

I have a class called storage. Header File:

#include <string>
using namespace std;
#include "Player.h"

class Storage {
public:
    static void Initialise();
    static string GetInformation();     
private:
    static Player player;
};

CPP File:

string Storage::GetInformation() {
    string returnString = "";
    // Get the players money
    // Convert it to a string
    string money("money");
    stringstream out;
    out << player.GetMoney();
    money = out.str();
    returnString += "Money: " + money + "\n";

    // Get the players ship information
    returnString += player.GetShipInformation();

    // Get the players current location
    returnString += player.GetCurrentLocation();

    return returnString;
}

void Storage::Initialise() {

}

This gives an error: "undefined reference to `Storage::player'". I've tried googling it and tweaking things, but I can't seem to find anything that works. If someone could point me in the right direction for an article to look at, that would be great, as I'm not sure of what the term is to search for to get the right answer.

Community
  • 1
  • 1
  • 3
    Remove the `using namespace std;` from the header. – Luchian Grigore Sep 24 '12 at 15:11
  • As an aside, you're already using an `ostringstream` to convert your money from an integer to a string. You should be using the same stringstream for the *entire* method. The whole method is made shorter, clearer and more performant by rewriting it as `stringstream out; out << "Money: " << money << std::endl << player.GetShipInformation() << player.GetCurrentLocation(); return out.str();` – user229044 Sep 24 '12 at 15:14

2 Answers2

6

You have declared the member, but not defined it.

You need, in e.g. Storage.cpp at the outermost level, i.e. at the same level as the method definitions:

Player Storage::player;
unwind
  • 391,730
  • 64
  • 469
  • 606
  • Thanks for that, I found that somewhere else but was putting it in the header file instead of the cpp, that has helped clear things up. – user1694806 Sep 24 '12 at 15:46
1

It is not enough to just declare a static class variable, it also need to define it, e.g. at the top of your .cpp file (but after includes, of course)

Player Storage::player;
Zdeslav Vojkovic
  • 14,391
  • 32
  • 45