2014-05-21 5 views
0

Ubuntu Linux 12.04 LTS에서 Qt 5.2.1을 사용하고 있습니다. (*) 표시 줄에 다음과 같은 오류와C++ 클래스 정적 멤버 초기화

#include "rtnamedinstance.h" 

// Initialize static members 
int RtNamedInstance::_nextInstanceNumber = 0; 
QMutex RtNamedInstance::_syncObj(QMutex::Recursive); 

RtNamedInstance::RtNamedInstance(QString instanceName) 
{ 
    QMutexLocker(&_syncObj); // (*) 

    // [... other code here ...] 
} 

컴파일러 종료 : 여기

class RtNamedInstance 
{ 
    // [... other code here ...] 

public: 
    static int _nextInstanceNumber; 
    static QMutex _syncObj; 
}; 

내 구현 (통화 당) : 여기 내 클래스 (.H)의 정의입니다

rtnamedinstance.cpp: In constructor 'RtNamedInstance::RtNamedInstance(QString)': rtnamedinstance.cpp:9:27: error: '_syncObj' declared as reference but not initialized

무엇이 누락 되었습니까?

+4

시작하려면'QMutexLocker' * 변수 *를 정의해야합니다. 지금은 * 임시 *'QMutexLocker' 인스턴스를 생성하는 표현식을 가지고 있습니다. 일단 표현식이 끝나면 (즉각적인) 인스턴스는 파괴되고 실제로 아무런 연산도하지 않는 식으로 아무것도 잠그지 않습니다. –

+0

@ JoachimPileborg : 나는 정말로 어리석은 소년이다 ... 나는 그걸로 몇 분을 낭비했다. 그러나 너는 옳다. QMutexLocker 변수 이름을 입력하는 것을 잊었다! 이제는 잘 작동합니다 ... 감사합니다! –

답변

1

@JoachimPileborg에 의해 제안, 나는 단순히이 혼란스러워 어떻게 든 컴파일러를 QMutexLocker 변수 이름을 입력 잊고 ...되었다 ...

올바른 코드입니다 (통화 당) :

#include "rtnamedinstance.h" 

// Initialize static members 
int RtNamedInstance::_nextInstanceNumber = 0; 
QMutex RtNamedInstance::_syncObj(QMutex::Recursive); 

RtNamedInstance::RtNamedInstance(QString instanceName) 
{ 
    QMutexLocker locker(&_syncObj); 

    // [... other code here ...] 
}