[英]C++ class static member initialization
我在Ubuntu Linux 12.04 LTS上使用Qt 5.2.1。 這是我的班級(.h)的定義:
class RtNamedInstance
{
// [... other code here ...]
public:
static int _nextInstanceNumber;
static QMutex _syncObj;
};
這是我的實現(.cpp):
#include "rtnamedinstance.h"
// Initialize static members
int RtNamedInstance::_nextInstanceNumber = 0;
QMutex RtNamedInstance::_syncObj(QMutex::Recursive);
RtNamedInstance::RtNamedInstance(QString instanceName)
{
QMutexLocker(&_syncObj); // (*)
// [... other code here ...]
}
編譯器退出,並在標記為(*)
行上出現以下錯誤:
rtnamedinstance.cpp:在構造函數'RtNamedInstance :: RtNamedInstance(QString)'中:rtnamedinstance.cpp:9:27:錯誤:'_syncObj'聲明為引用但未初始化
我想念什么?
正如@JoachimPileborg所建議的那樣,我只是忘記鍵入QMutexLocker變量名...而這在某種程度上使編譯器感到困惑...
正確的代碼是(.cpp):
#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 ...]
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.