This question has already been answered:

QT Creator. There is a class:

class Option: public QObject { Q_OBJECT public: static int getSizeHistory(); Option(); ~Option(); private: static int sizeHistory; }; 

Its implementation is in option.cpp. From another class, I call its static method:

 void MainWindow::on_actionOpen_triggered() { Option::getSizeHistory(); } 

The compiler gives an error:

mainwindow.obj: -1: error: LNK2019: unresolved external symbol "public: static int cdecl Option :: getSizeHistory (void)" (? getSizeHistory @ Option @@ SAHXZ) referenced in function "private: void thiscall MainWindow :: on_actionOpen_triggered ( void) "(? on_actionOpen_triggered @ MainWindow @@ AAEXXZ)

Reported as a duplicate by the member αλεχολυτ c ++ Jan 18 at 10:11 .

A similar question was asked earlier and an answer has already been received. If the answers provided are not exhaustive, please ask a new question .

  • > Its implementation in option.cpp The option.cpp code in the studio. And he generally links in the project? - user6550
  • File "option.cpp" added to project. Here is an implementation of this method: int Option :: getSizeHistory () {return Option :: sizeHistory; } - Vadym Demchuk
  • @vaddemgen: added is good. And yet, he links? Maybe you somehow added it wrong. Or for example they nakhimichili with namespace'ami. Or something else. Put somewhere the entire file, in the above fragment of problems is not visible. - VladD
  • and there is. forgot to initialize the static member of the class. Thank you) - Vadym Demchuk

1 answer 1

Perhaps you forgot to initialize a static member of a class before using it (try static int sizeHistory = 0;)