2012-03-13 3 views
1

다음 클래스의 ID를 추적하기 위해 정적 멤버 변수 id_index를 사용하여 "Person"클래스를 만들려고합니다. 동일한 ID). 나는이 프로그램을 실행하면 나는 이러한 오류를 얻을 :클래스 ID를 추적하기 위해 ID 정적 멤버 변수 만들기

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 파일입니다

#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) { 

} 

주요 기능입니다. cpp :

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

답변

5

두 번째로 person.h 파일의 마지막 행인 unsigned int Person::id_index = 0;은 person.cpp에 속합니다. 통화 당에 속하는 기능정의 정적 변수 정의 (즉, 광고가 무엇 인)도 통화 당에 속하는 방법에 유사

.