0

Possible Duplicate:
static members and LNK error in C++
What does it mean to have an undefined reference to a static member?

I have this class:

class A_GItem  
{
public:        
void create_item();    
private:
  static int static_index;  
}

The create_item function is as simply as:

void create_item() { static_index++; }

When compile (after a clean, trying to solve the problem ) I have : error LNK2001: unresolved external symbol "private: static int A_GItem::static_index

Any idea ? Thanks

Community
  • 1
  • 1
tonnot
  • 435
  • 1
  • 5
  • 17

2 Answers2

3

Member static variables require initialization. You are merely declaring your variable in your header, but not defining it:

//A_GItem.h
class A_GItem  
{
public:        
void create_item();    
private:
  static int static_index;  
}

//A_GItem.cpp
int A_GItem::static_index = 0;
Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
2

You have to actually define the static member varible

int A_GItem::static_index;

in one of .cpp files.

sharptooth
  • 167,383
  • 100
  • 513
  • 979