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?

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
Ken Li
  • 2,578
  • 4
  • 25
  • 26

3 Answers3

24

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.

Nawaz
  • 353,942
  • 115
  • 666
  • 851
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.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
2

Consider putting

bool CommandManager::started;

where you define other members.

Michael Krelin - hacker
  • 138,757
  • 24
  • 193
  • 173