提问人:CybeX 提问时间:6/11/2020 更新时间:6/11/2020 访问量:166
对不同类中静态成员 quint64/QMutex 的未定义引用
undefined reference to static members quint64/QMutex in separate classes
问:
我有几个类给出了这个奇怪的未定义参考问题。以下是两个示例:
backupmanager.h
class BackupManager : public QObject {
Q_OBJECT
const QString TAG = QString("BackupManager");
public:
//...
signals:
//...
public slots:
//...
private:
// Intermediatory storage map for file information
static quint64 bytesSavedDedulication; <-------------- static member
static quint64 bytesSavedLocalDuplication; <-------------- static member
static quint64 bytesSavedGlobalDuplication; <-------------- static member
static quint64 numberOfFilesShouldBeCopied; <-------------- static member
//...
}
备份管理器.cpp
//...
path\to\project\libs\backupmanager.cpp:720: error: undefined reference to `BackupManager::bytesSavedLocalLock'
path\to\project\libs\backupmanager.cpp:721: error: undefined reference to `BackupManager::bytesSavedLocalDuplication'
//...
再举一个例子
数据库管理器.h
class DatabaseManager {
//...
private:
// Mutex preventing concurrent access
static QMutex dbLock; <-------------notice that it is static
//...
}
数据库管理器.cpp
path\to\project\libs\databasemanager.cpp:431: error: undefined reference to `DatabaseManager::dbLock'
静态成员在尝试在代码中访问它们时会提供未定义的引用,如您在 cpp 行引用中看到的那样。
根据经验,我知道未定义的引用通常意味着头文件中的方法声明没有定义,但这是我以前还没有遇到过的问题。
这篇文章建议尝试定义成员。这样做是为了:static quint64 bytesSavedDedulication;
static quint64 bytesSavedDedulication = 0;
给出以下错误:
path\to\project\libs\backupmanager.h:312: error: non-const static data member must be initialized out of line
什么原因可能导致此问题?
(如果需要更多信息,请告诉我 - 很乐意提供)
答: 暂无答案
评论
quint64 BackupManager::bytesSavedDedulication = 0;
quint64 BackupManager::bytesSavedLocalDuplication = 0;