c++ - 访问静态变量时未解析的外部符号

标签 c++ linker-errors

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);
};

这是客户端代码:

CommandManager::started = true;

将这两个文件链接在一起我得到:

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

我哪里错了?

最佳答案

你做错了。

class CommandManager {

public:
    void sendText(std::string command);
    static bool started; //NOT this -> bool CommandManager::started
    //...
};

然后将静态成员的定义放在.cpp文件中为:

#include "CommandManager.h" //or whatever it is

bool CommandManager::started = true; //you must do this in .cpp file

现在您可以在客户端代码中使用 CommandManager::started

关于c++ - 访问静态变量时未解析的外部符号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8383330/

相关文章:

c++ - opencv 项目中未处理的异常

c++ - 继承 - 使用指针输出

c# - VC++ 在解决方案中从非/clr 项目的函数调用/clr 项目的函数

ios - 在iOS中一起使用Dropbox Core API及其数据存储区API时出现链接器错误

c++ - 你如何为 x64 编译静态 pthread-win32 库?

模板和非模板函数之间的 C++ 重载解析

c++ - ERROR LNK1104 - .`obj` 文件没有任何文件名

ios - 在项目中使用 pod 安装 Facebook 时出现 Apple Mach-O 链接器错误

c++ - 如何根据数组元素固定我的循环| C++

c++ - qInitResources() 上的 Qt 链接器错误 LNK2019 - RCC