在Visual Studio中编码时,我得到了一个未解决的外部符号错误
我不知道该怎么办我不知道怎么了。
你能帮我破译一下吗?我应该在哪里寻找什么样的错误?
1>Form.obj : error LNK2019: unresolved external symbol "public: class Field * __thiscall Field::addField(class Field *)" (?addField@Field@@QAEPAV1@PAV1@@Z) referenced in function "public: void __thiscall Form::parse(class std::basic_stringstream<char,struct std::char_traits<char>,class std::allocator<char> > &)" (?parse@Form@@QAEXAAV?$basic_stringstream@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z)
1>Form.obj : error LNK2019: unresolved external symbol "public: virtual void __thiscall Field::parse(class std::basic_stringstream<char,struct std::char_traits<char>,class std::allocator<char> > &)" (?parse@Field@@UAEXAAV?$basic_stringstream@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z) referenced in function "public: __thiscall InputField::InputField(class std::basic_stringstream<char,struct std::char_traits<char>,class std::allocator<char> > &)" (??0InputField@@QAE@AAV?$basic_stringstream@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z)
1>Form.obj : error LNK2001: unresolved external symbol "public: virtual void __thiscall Field::prompt(void)" (?prompt@Field@@UAEXXZ)
1>Form.obj : error LNK2001: unresolved external symbol "public: virtual class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __thiscall Field::getName(void)" (?getName@Field@@UAE?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@XZ)
1>Form.obj : error LNK2001: unresolved external symbol "public: virtual class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __thiscall Field::getType(void)" (?getType@Field@@UAE?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@XZ)
1>Form.obj : error LNK2001: unresolved external symbol "public: virtual void __thiscall Field::describe(void)" (?describe@Field@@UAEXXZ)
1>C:\Users\tomy\Documents\Visual Studio 2010\Projects\zapoctovkac++\Debug\zapoctovkac++.exe : fatal error LNK1120: 6 unresolved externals
在我的情况下,我有多个名称空间在我的头文件中没有嵌套(一个接一个),但在我的源文件中,我不小心嵌套了一个名称空间在另一个:
// myExample.h
#ifndef MYEXAMPLE_H
#define MYEXAMPLE_H
namespace A
{
void aFunc();
}
namespace B
{
void bFunc();
}
// myExample.cpp
#include "myExample.h"
namespace A
{
void aFunc()
{
...
}
namespace B // whoops! I nested this inside namespace A when I didn't mean to.
{
void bFunc()
{
...
}
}
}
// main.cpp
#include "myExample.h"
int main()
{
myExample::B::bFunc();
return 0;
}
当我在Main中使用F12对函数“Go to definition”时,Visual Studio在源文件中找到了代码,尽管它是在一个更深层的命名空间中声明的。
调试技术
我在尝试调试问题时重命名函数时发现了这个问题。重命名预览窗口显示了一个“External References”节点,其中源文件中的函数意外地嵌套在另一个名称空间下。
我在我的项目中遇到了同样的错误,我已经设法解决了。
这个问题把我带到了这里,但我发现这是visual studio本身的一个构建文件问题。
如果你不能在你的代码中找到任何错误,并且你做了以上所有的事情,我建议你在你的项目中寻找。vcxproj文件,检查它是否包括你的MyClass.cpp和源文件
SynthProject.vcxproj
<ItemGroup>
<ClCompile Include="main.cpp" />
<ClCompile Include="myClass.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="myClass.h" />
</ItemGroup>
SynthProject.vcxproj.filters
<ItemGroup>
<ClCompile Include="main.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="myClass.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="myClass.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
而不是像
SynthProject.vcxproj
<ItemGroup>
<ClCompile Include="main.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="myClass.h" />
<ClInclude Include="myClass.cpp" />
</ItemGroup>
SynthProject.vcxproj.filters
<ItemGroup>
<ClCompile Include="main.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="myClass.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="myClass.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="myClass.cpp">
<Filter>Source Files</Filter>
</ClInclude>
</ItemGroup>
我已经设法手动做到这一点,但建议卸载并将文件添加回项目中,如果你没有,或不使用git。
是什么导致了我的这种情况:
我有一个很大的文件Foo.cpp,没有Foo.h。Foo.cpp的开头是这样的:
// ... 100 LOC here ...
namespace NS {
// ... 100 more LOC here ...
static int var;
我删除了“static”关键字,并添加了一个Foo.h:
extern int var;
你看到错误了吗?
我完全忽略了var最初是在名称空间中定义的,因为名称空间声明隐藏在其他代码中。修复是这样改变extern:
namespace NS {
extern int var;
}
此错误可能是由于将模板类的函数定义放在单独的.cpp文件中引起的。对于模板类,成员函数必须在头文件中声明。可以通过在.h文件中定义成员函数内联或紧跟类定义之后来解决这个问题。
例如,不像其他类那样将函数定义放在.cpp文件中,你可以像这样内联定义它们:
template<typename T>
MyClassName {
void someFunction() {
// Do something
...
}
void anotherFunction() {
// Do something else
...
}
}
或者你可以在类定义之后定义它们,但在同一个文件中,像这样:
template<typename T>
MyClassName {
void someFunction();
void anotherFunction();
}
void MyClassName::someFunction() {
// Do something
...
}
void MyClassName::anotherFunction() {
// Do something else
...
}
我只是想分享一下,因为似乎没有人提到模板类。这就是我的案例中出现错误的原因。