2012-03-13 16 views
1

私は、作成された次のクラスのIDを追跡するために静的メンバー変数id_indexを持つ "Person"クラスを作成しようとしています。同じID)。私はプログラムを実行すると、私はこれらのエラーを取得:ID静的メンバー変数を作成してクラスIDを追跡する

#ifndef PERSON_H_ 
#define PERSON_H_ 
#include <iostream> 

class Person { 
public: 
    static unsigned int id_index; // will aut0-set to 0 
    Person(); 
}; 
unsigned int Person::id_index = 0; 
#endif /* PERSON_H_ */ 

person.cpp:人間の内部に格納さ

Person::Person(): ID(id_index) { 

} 

主な機能

G:\WorkSpace\Human\Debug/../Person_Class/Person.cpp:11: multiple definition of `Person::id_index' 
Person_Class\Person_Set.o:G:\WorkSpace\Human\Debug/../Person_Class/Person_Set.cpp:10: first defined here 
Human.o: In function `main': 
G:\WorkSpace\Human\Debug/../Human.cpp:16: multiple definition of `Person::id_index' 
Person_Class\Person_Set.o:G:\WorkSpace\Human\Debug/../Person_Class/Person_Set.cpp:10: first defined here 

これはperson.hファイルです。 cpp:

int main(){ 
    Person michael; 
    return 0; 
} 

答えて

5

2番目のperson.hファイルの最後の行で、 unsigned int Person::id_index = 0;がperson.cppに属しています。 .CPPに属する機能定義は、(その行が何であるかである)静的変数定義も.CPPに属する方法と同様

関連する問題