2013-12-20 1 views
0

나는 singalton 클래스 예제를 사용하고 있습니다.링커 오류 받기

error LNK2001: unresolved external symbol "public: static class CConfig * CConfig::config" ([email protected]@@[email protected]) 
SingaltonClass.exe : fatal error LNK1120: 1 unresolved externals 

:

#include "stdafx.h" 
#include "SingaltonClass.h" 

void hello(); 

CConfig* 
CConfig :: initilize_config() 
{ 
    if (config == 0) 
    { 
     config = new CConfig(); 
    } 
    return config; 
} 

int _tmain(int argc, _TCHAR* argv[]) 
{ 
    CConfig *cconfig = CConfig::initilize_config(); 
    cconfig->displayStatus(); 

    hello(); 
    return 0; 
} 

void hello() 
{ 
    CConfig *config = CConfig::initilize_config(); 
    config->displayStatus(); 
} 

내가 점점 오전 오류 다음 비주얼 스튜디오 10에서 컴파일에 .. 여기 은 .H 파일

#ifndef __SINGALTON_CLASS__ 
#define __SINGALTON_CLASS__ 

#include "iostream" 

class CConfig 
{ 
private: 
    CConfig(){}; 

private: 
    bool status; 
    static CConfig* config; 

public: 
    static CConfig* initilize_config(); 
    ~CConfig() 
    { 
    } 
    void displayStatus() 
    { 
     std::cout << "Status:"<<status; 
    } 
}; 

#endif //__SINGALTON_CLASS__ 

이며, 여기에 .cpp 파일의 코드는 나는 정확히 어디서 문제인지 이해할 수 없다. 답장을 부탁한다.

답변

1

당신은 당신의 헤더에있는 정적 멤버를 선언,하지만 당신은 그것을 정의하지 않았다

CConfig* CConfig::config = NULL; 

from here을 인용 :

class CConfig 
{ 
    // ... blah blah ... 
private: 
    static CConfig* config; 

    // ... blah blah ... 
}; 

당신은 또한 CPP 파일에 대한 변수를 정의 할 필요가

9.4.2 Static data members
The declaration of a static data member in its class definition is not a definition and may be of an incomplete type other than cv-qualified void. The definition for a static data member shall appear in a namespace scope enclosing the member's class definition. In the defi- nition at namespace scope, the name of the static data member shall be qualified by its class name using the :: operator. The initializer expression in the definition of a static data member is in the scope of its class (basic.scope.class).

+0

나는 그 변수를 초기화하지만 여전히 내가 같은 오류가 무엇입니까 .. 작동합니다. –

+0

@RavindraGupta 새 코드로 질문을 업데이트하십시오. 귀하의 질문에 여전히 정의가없는 소스 코드가 있습니다. – manuell

0

해결 방법 : 클래스 외부에서 정적 변수를 정의하지 못했습니다. 는 SO 여기에 헤더 파일 자체에서 나는 마지막에 한 줄을 추가하고이

CConfig* CConfig::config;