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
  • 369,052
  • 73
  • 620
  • 1,021
Ken Li
  • 2,548
  • 4
  • 24
  • 25

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
  • 341,464
  • 111
  • 648
  • 831
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
  • 216,767
  • 17
  • 275
  • 509
2

Consider putting

bool CommandManager::started;

where you define other members.

Michael Krelin - hacker
  • 131,515
  • 23
  • 189
  • 171