Jump to content
  • Advertisement
Sign in to follow this  
saeedm

simple static question

This topic is 3858 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

In my class Vertex I'd like to keep a static variable std::vector<int> dists, and manipulate it from the functions in Vertex. The solutions I have found online are giving me errors (Im working in dev-c++ with Mingw/GCC). Here is what I have tried: //Vertex.h// class Vertex { private: static std::vector<int> dist; public: Vertex(int); int getDist(); }; //Vertex.cpp// Vertex::Vertex(int i) { Vertex::dist = i; } int Vertex::getDist() { return Vertex::dist; I get a linker error - undefined reference to Vertex::dists Thanks for any help

Share this post


Link to post
Share on other sites
Advertisement
Short answer:
In your CPP file you must put:
std::vector<int> Vertex::dist;

Long answer:
Writing "static type name;" in a header file tells the compiler that there is a static variable of called 'name' *somewhere* that it can use. But this doesn't tell it where to allocate the storage space for the variable.
You must put the above (short-answer) code in a source file, which tells the compiler which code-module the variable's storage will be located. Otherwise, as you've discovered, when the linker tries to assemble your program, it wont be able to find where that variable is supposed to be stored ;)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!