Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 477
Ken Li Avatar asked Dec 05 '11 09:12

Ken Li


People also ask

What is unresolved external symbol?

Unresolved external references occur when the symbol for a function or global variable is referenced in a program, but none of the object files or libraries specified in the link step contain a definition for that symbol.

How do I fix unresolved external symbol lnk2001?

To fix this issue, add the /NOENTRY option to the link command. This error can occur if you use incorrect /SUBSYSTEM or /ENTRY settings in your project. For example, if you write a console application and specify /SUBSYSTEM:WINDOWS, an unresolved external error is generated for WinMain .

What is an undefined reference unresolved external symbol error and how do I fix it?

So when we try to assign it a value in the main function, the linker doesn't find the symbol and may result in an “unresolved external symbol” or “undefined reference”. The way to fix this error is to explicitly scope the variable using '::' outside the main before using it.

How do you declare a static member variable in C++?

We can define class members static using static keyword. When we declare a member of a class as static it means no matter how many objects of the class are created, there is only one copy of the static member. A static member is shared by all objects of the class.


2 Answers

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.

like image 117
Nawaz Avatar answered Oct 04 '22 20:10

Nawaz


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.

like image 28
Basile Starynkevitch Avatar answered Oct 04 '22 21:10

Basile Starynkevitch