我在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: In构造函数"RtNamedInstance:: RtNamedInstance (QString)":rtnamedinstance.cpp: 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 ...]
}