c++类静态成员初始化
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: 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 ...]
}
相关文章:
- C - 初始化静态成员的方法
- 如何在运行时使用静态成员函数初始化静态成员变量
- 如何在班级中声明和初始化静态成员
- 初始化静态成员使编译工作.但是为什么
- 使用作用域内生成的指针初始化静态成员
- 初始化静态成员类(用于信号处理程序)
- 可以通过成员初始化列表初始化静态成员变量
- 如何用功能指针作为参数初始化静态成员的模板
- 运行主后,是否可以初始化静态成员变量
- 在C++中初始化静态成员
- 必须初始化静态成员
- 初始化静态成员变量值的正确位置
- 初始化静态成员函数指向同名函数的指针
- 如何初始化静态成员变量"dynamically"?
- 如何用函数的结果初始化静态成员数组
- 是否可以动态初始化静态成员数组?
- 初始化静态成员时重声明错误
- 避免在初始化静态成员时使用new()
- LNK 2001:初始化静态成员时未解析的外部符号
- 如何在c++中重写=操作符时初始化静态成员