0

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)

CybeX
  • 2,060
  • 3
  • 48
  • 115
  • Have you tried `quint64 BackupManager::bytesSavedDedulication = 0;` in the .cpp file? – dxiv Jun 10 '20 at 23:00
  • @dxiv so that works, but it is local only to that class - so getters & setters won't work with it...right? – CybeX Jun 10 '20 at 23:06
  • You declared it as a static private member, if that's what you mean by "*local only to that class*". You *can* write getters and setters for private members, though not sure I follow that part of the question. – dxiv Jun 10 '20 at 23:11
  • Just a common C++ question? If so then you need to initialize those static data member like `quint64 BackupManager::bytesSavedLocalDuplication = 0;` in backupmanager.cpp. – Alexander V Jun 11 '20 at 00:36

0 Answers0