I create a class, it has 2 methods and static properties for storing data.
Task: there are 2 classes, one of them changes the values ​​of a static class, and the second reads.

I connect it:

#include <userdata.h> 

Static class:

 class UserData { public: static QString TOKEN; static QString UID; static void setToken ( QString token ); static void setUID ( QString uid ); struct MusicData { QString title; QString url; int id; }; static int COUNT; struct Music { MusicData *n = new MusicData[COUNT]; }; static Music musicList; }; 

When accessing its property, UserData::musicList.n[i].id = i; writes an error:

error: undefined reference to `UserData :: musicList '

Also when describing methods

 static void UserData::setToken ( QString token ) { TOKEN = token; } static void UserData::setUID(QString uid) { UID = uid; } 

also writes an error:

error: cannot declare member function 'static void UserData :: setToken (QString)' to have static linkage [-fpermissive]
static void UserData :: setToken (QString token) {

PS Do not hit. ) ^

  • In which file are you trying to define methods? - ASten

1 answer 1

Make the class a singleton, solve all problems at once =)

Found the problem) in UserData.cpp add

 UserData::Music UserData::musicList; int UserData::COUNT; 

Static class members must also be described in the .cpp file.