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

时间:2016-01-27 19:54:03

标签: 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();
};

1 个答案:

答案 0 :(得分:2)

您的实施文件不应该重新定义该课程。这是一个可能的(正确的)代码:

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...
}