5
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?

4
  • 3
    possible duplicate of Unresolved external symbol on static class members Commented Dec 5, 2011 at 9:40
  • I looked at that question and the solution did not help me Commented Dec 5, 2011 at 9:44
  • @KenLi: Try as I said in my answer. And let me know if you still face problem. Commented Dec 5, 2011 at 9:53
  • @KenLi - the other question is about the exact same issue - you declared but did not define your static in your class. Commented Dec 5, 2011 at 10:27

3 Answers 3

25

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.

Sign up to request clarification or add additional context in comments.

Comments

4

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.

Comments

2

Consider putting

bool CommandManager::started;

where you define other members.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.