static data member

G

Guest

what is meaning of this? (MS VC++ .NET)
----------------------
Linking...
machine.obj : error LNK2001: unresolved external symbol "private: static class std::vector<struct GessoidMachine::Resolution,class
std::allocator<struct GessoidMachine::Resolution> > GessoidMachine::Machine::resolution"
(?resolution@Machine@GessoidMachine@@0V?$vector@UResolution@GessoidMachine@@V?$allocator@UResolution@GessoidMachine@@@std@@@std@@A)
machine.obj : error LNK2001: unresolved external symbol "private: static struct SDL_Surface * GessoidMachine::Machine::screen"
(?screen@Machine@GessoidMachine@@0PAUSDL_Surface@@A)
machine.obj : error LNK2001: unresolved external symbol "private: static int GessoidMachine::Machine::resolution_index"
(?resolution_index@Machine@GessoidMachine@@0HA)
..\Debug/main.exe : fatal error LNK1120: 3 unresolved externals
----------------------

The error is in this code: (All member functions & data are static because I want only one instance of "Machine" in my project and I
want access in any data member without this->
----------------------
class Machine {
public:
static member1(arg1, arg2);
static member2();
.......
private:
// handle all resolutions supported from system
static vector <Resolution> resolution;
static int resolution_index;
static SDL_Surface *screen;
};
----------------------
 
J

John Carson

what is meaning of this? (MS VC++ .NET)
----------------------
Linking...
machine.obj : error LNK2001: unresolved external symbol "private:
static class std::vector<struct GessoidMachine::Resolution,class
std::allocator<struct GessoidMachine::Resolution> >
GessoidMachine::Machine::resolution"
(?resolution@Machine@GessoidMachine@@0V?$vector@UResolution@GessoidMachine@@
V?$allocator@UResolution@GessoidMachine@@@std@@@std@@A)
machine.obj : error LNK2001: unresolved external symbol "private:
static struct SDL_Surface * GessoidMachine::Machine::screen"
(?screen@Machine@GessoidMachine@@0PAUSDL_Surface@@A)
machine.obj : error LNK2001: unresolved external symbol "private:
static int GessoidMachine::Machine::resolution_index"
(?resolution_index@Machine@GessoidMachine@@0HA) .\Debug/main.exe :
fatal error LNK1120: 3 unresolved externals
----------------------

The error is in this code: (All member functions & data are static
because I want only one instance of "Machine" in my project and I
want access in any data member without this->
----------------------
class Machine {
public:
static member1(arg1, arg2);
static member2();
.......
private:
// handle all resolutions supported from system
static vector <Resolution> resolution;
static int resolution_index;
static SDL_Surface *screen;
};
----------------------

static data members are like member functions: you declare them in the
header file and then define them in the .cpp file. Basically, storage is
allocated for ordinary data members when you define an object of the class.
But static data members are independent of any class object and hence you
need to define them separately in order for storage to be allocated for
them.

Thus your .cpp file needs to contain:

vector <Resolution> Machine::resolution;
int Machine::resolution_index;
SDL_Surface *Machine::screen;

Note that you must omit the static keyword in these definitions.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,241
Members
46,831
Latest member
RusselWill

Latest Threads

Top