简体   繁体   中英

unresolved external symbol 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's the client code:

CommandManager::started = true;

Linking these two files together I get:

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

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

Where did I go wrong here?

You're doing that incorrectly.

class CommandManager {

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

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

#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.

You should have inside your class:

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

and outside your class, in a *.cc file (not in a *.hh header file), a definition like

bool CommandManager::started;

BTW, I believe you'll better make that private .

Consider putting

bool CommandManager::started;

where you define other members.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM