I have a few classes giving this strange undefined reference problem. Here are two examples to start off with :
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
//...
}
backupmanager.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'
//...
and another example
DatabaseManager.h
class DatabaseManager {
//...
private:
// Mutex preventing concurrent access
static QMutex dbLock; <-------------notice that it is static
//...
}
DatabaseManager.cpp
path\to\project\libs\databasemanager.cpp:431: error: undefined reference to `DatabaseManager::dbLock'
The static members give the undefined reference when attempting to access them in code, as you see in the cpp line references.
From experience, I know that an undefined reference usually means a method declaration in the header file has no definition, but this is a problem which I have not yet run into before.
This post suggests attempting to defined the member. Doing this for static quint64 bytesSavedDedulication;
:
static quint64 bytesSavedDedulication = 0;
gives the following error:
path\to\project\libs\backupmanager.h:312: error: non-const static data member must be initialized out of line
What could cause this problem?
(if more information is required, please let me know - would be happy to provide it)