0

I am trying to implement a new feature in an existing QT/c++ project. In this project a header file and a .cpp file were defined. I add two methods (setter and getter) in a .cpp file and it works great. Now, I'm trying to clean up my code and add this methods to the existing .h and .cpp files, but I'm getting a lot of errors and I don't know how to solve it.

In my .h file I have a static method and in this method I want to call a setter method (called in the .cpp file) and then call a getter method in another static method. In the .h file I have the following static variable static std::map<std::string, std::string> data. Also in the header file I define the getter and setter methods, but when I try to call the setter method (in the .cpp) I get the following error:

qt_gui.lib(Paste.cpp.obj):-1: error: LNK2001: unresolved external symbol "private: static class std::map<class std::basic_string<char,struct std::char_traits<char>,class std ::allocator<char> >,class archx::Component,struct std::less<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > >,class std ::allocator<struct std::pair<class std::basic_string<char,struct...

I know that if I want to call methods in a static method, it must also be static. I already declared the map as static and the setter method as static. But I'm still getting these errors. The only way the function works is to declare a the map as a global variable in the .cpp file and also create the getter and setter method (without declaring in the header file), but I think this is not the best practice, because I have a .h file.

Sorry if this is a basic question about c++. But I don't know how to solve it.

Thanks in advance!

BadRobot
  • 265
  • 1
  • 9
  • 1
    Basically, you either didn't defined the variable, or you don't compile the cpp file, or you don't link to the library containing the definition. There is a limited number of reason this can happen. – Guillaume Racicot Sep 27 '22 at 01:55
  • Hi @GuillaumeRacicot, In the .cpp file all works well, but when I add the map in the .header file I get the errors I mention. If I add the `className` the QT IDE suggest me to remove the className. – BadRobot Sep 27 '22 at 02:01
  • @GuillaumeRacicot either way, qualified or not, stating `static` in the .cpp file means it can only be used within that .cpp file. Take off the `static` and you'll be fine, because the declaration in the class will still be static. – Mark Ransom Sep 27 '22 at 02:08
  • @MarkRansom, yes as you mention, If I remove the `static` in the .cpp all works well. But I'm trying to add this in a .header file, here I get a lot of errors. The first one is `Call to non-static member function without an object argument` – BadRobot Sep 27 '22 at 02:14
  • 1
    You must keep the static in the header, but remove static in the cpp. @BadRobot – Guillaume Racicot Sep 27 '22 at 10:30
  • @BadRobot static in a class and static out of class has completely and opposite different meanings. – Guillaume Racicot Sep 27 '22 at 10:31
  • 1
    Specifically this answer in the duplicate: https://stackoverflow.com/a/62112220/2104697 – Guillaume Racicot Sep 27 '22 at 10:52

0 Answers0