Unresolved external character when accessing a static variable

class CommandManager { public: void sendText(std::string command); static bool CommandManager::started; private: bool parseCommand(std::string commands); void changeSpeed(std::vector<std::string> vec); void help(std::vector<std::string> vec); }; 

And here is the client code:

 CommandManager::started = true; 

By linking these two files together, I get:

1> UAlbertaBotModule.obj: error LNK2001: unresolved external character "public: static bool CommandManager :: started" (? Launched @CommandManager @@ 2_NA)

1> C: \ Development \ School \ cmput350-uofabot \ UAlbertaBot \ vs2008 \ Release \ UAlbertaBot.dll: fatal error LNK1120: 1 unresolved external

Where am I wrong here?

+4
source share
3 answers

You are doing it wrong.

 class CommandManager { public: void sendText(std::string command); static bool started; //NOT this -> bool CommandManager::started //... }; 

then put the definition of the static member in the .cpp file as follows:

 #include "CommandManager.h" //or whatever it is bool CommandManager::started = true; //you must do this in .cpp file 

Now you can use CommandManager::started in your client code.

+21
source

You must have inside your class:

 class CommandManager { public: void sendText(std::string command); static bool started; //// etc }; 

and outside of your class, in the *.cc file (not in the header *.hh file), type definition

 bool CommandManager::started; 

By the way, I believe that you are better off making it private .

+4
source

Consider putting

 bool CommandManager::started; 

where you define other members.

+2
source

Source: https://habr.com/ru/post/1384666/


All Articles