C++ 未解析的外部符号?

标签 c++ linker linker-errors

<分区>

在 VS 中处理我的项目时,我决定将源代码拆分成单独的文件。然而,在尝试编译之后,我得到了这个错误:

LNK2019 unresolved external symbol "public: __thiscall CS_RawUser::CS_RawUser(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,int)" (??0CS_RawUser@@QAE@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@0H@Z) referenced in function "void __cdecl mkUser(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (?mkUser@@YAXV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@00@Z)

我在谷歌上搜索,发现了多个类似的问题。但是,对于缺少的实现,他们所有人都有相同的答案。然而,在我的源代码中(除非我大错特错)错误中提到的函数已正确定义。

CS_OBJ.cpp:

class CS_RawUser
{
private:
    string m_username;
    string m_password;
    int m_level;

string compileDatabaseEntry()
{
    return "CSEC_INSTANCE_USER:" + m_username + "," + m_password + "," + to_string(m_level);
}

public:
    CS_RawUser(string username, string password, int level)
    {
        m_username = username;
        m_password = password;
        m_level = level;
    }
    string username() { return m_username; }
    string password() { return m_password; }
    int level() { return m_level; }
    string compile() { return compileDatabaseEntry(); }
};

CS_OBJ.h:

class CS_RawUser
{
private:
    std::string m_username;
    std::string m_password;
    int m_level;
    std::string compileDatabaseEntry();
public:
    CS_RawUser(std::string username, std::string password, int level);
    std::string username();
    std::string password();
    int level();
    std::string compile();
};

最佳答案

您的实现文件不应该重新定义类。下面是一个可能的(正确的)代码:

CS_OBJ.h:

class CS_RawUser
{
public:
    CS_RawUser(std::string username, std::string password, int level);
};

CS_OBJ.cpp

#include "CS_OBJ.h"

CS_RawUser::CS_RawUser(std::string username, std::string password, int level)
{
    ... // initialization here...
}

关于C++ 未解析的外部符号?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35046806/

相关文章:

c++ - 什么是 undefined reference /未解析的外部符号错误,我该如何解决?

python - 将 Boost.Python 与 g++ 的问题联系起来

带有模板编译错误的 C++ 类

c++ - 类实例成员初始化

c# - 为什么 EmguCV 高斯模糊可能不会返回与 OpenCV 高斯模糊相同的结果?

c++ - 在 Visual Studios 中创建 sqlite3.lib 文件/使用 sqlite3

c - avr-ld : avr:51 architecture of input file `main.o' is incompatible with avr output

c - 从 RAM ARM Cortex M4 执行

c++ - CMake 没有将共享库的链接依赖项传播到我的可执行文件

c++ - 我可以在 C++ 中有一个 ifstream 数组吗