C++ 未解析的外部符号?
c++ unresolved external symbol?
在 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)
我在 google 上搜索并发现了多个类似的问题。但是,对于缺少的实现,他们所有人都有相同的答案。然而,在我的源代码中(除非我大错特错)错误中提到的函数已正确定义。
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();
};
您的实施文件不应重新定义 class。这是可能的(正确的)代码:
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...
}
在 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)
我在 google 上搜索并发现了多个类似的问题。但是,对于缺少的实现,他们所有人都有相同的答案。然而,在我的源代码中(除非我大错特错)错误中提到的函数已正确定义。
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();
};
您的实施文件不应重新定义 class。这是可能的(正确的)代码:
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...
}