Ken Li
Ken Li

Reputation: 2618

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?

Upvotes: 5

Views: 25690

Answers (3)

Sarfaraz Nawaz
Sarfaraz Nawaz

Reputation: 361702

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.

Upvotes: 25

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.

Upvotes: 4

Michael Krelin - hacker
Michael Krelin - hacker

Reputation: 143259

Consider putting

bool CommandManager::started;

where you define other members.

Upvotes: 2

Related Questions