2011-11-15 14 views
0

可能性の重複をコンパイルしません:
static members and LNK error in C++
What does it mean to have an undefined reference to a static member?C++静的変数。

私はこのクラスを持っている:

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

create_item機能は、単にようである:

void create_item() { static_index++; } 

コンパイルが(クリーン後、問題を解決しようとしている)私が持っている: エラーLNK2001:未解決の外部シンボル「プライベート:静的int型A_GItem :: static_index

を任意のアイデア?おかげ

答えて

3

メンバ静的変数には初期化が必要です。ヘッダーに変数を宣言しているだけで、定義することはできません。

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

//A_GItem.cpp 
int A_GItem::static_index = 0; 
2

は、あなたが実際の.cppファイルの一つで

int A_GItem::static_index; 

静的メンバvaribleを定義する必要があります。