在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

当前回答

我有同样的链接错误,但从一个测试项目引用另一个dll。发现在错误消息中指定的每个函数前面添加_declspec(dllexport)后,链接正常工作。

其他回答

它看起来缺少一个库或包含,你可以尝试找出你的库的什么类有getName, getType等…然后放到头文件中或者使用#include。

此外,如果这些碰巧来自外部库,请确保在项目文件中引用它们。例如,如果这个类属于abc。lib然后在你的Visual Studio

单击Project Properties。 进入配置属性,C/ c++, 生成,验证你指向abc。附加库位置 包括目录。在链接器,输入,确保你有 美国广播公司(abc)。附加依赖项下的lib。

在我的情况下,我有多个名称空间在我的头文件中没有嵌套(一个接一个),但在我的源文件中,我不小心嵌套了一个名称空间在另一个:

// 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。

确保您没有试图将插入或提取操作符作为内联函数重载。我有这个问题,它只消失了,当我删除了那个关键字。

除了上面Chris Morris的精彩回答之外,我还发现了一个非常有趣的方法,如果你调用一个没有被设置为pure但没有自己实现的虚方法,你可以收到同样的错误。这是完全相同的原因(编译器无法找到方法的实现,因此出错),但我的IDE丝毫没有捕捉到这个错误。

例如,下面的代码将得到一个带有相同错误消息的编译错误:

//code testing an interface
class test
{
   void myFunc(); 
}

//define an interface
class IamInterface
{
    virtual void myFunc();
}

//implementation of the interface
class IamConcreteImpl
{
    void myFunc()
    {
       1+1=2;
    }
}

但是,将IamInterface myFunc()更改为纯虚方法(“必须”实现的方法,而不是“可以”重写的虚方法)将消除编译错误。

//define an interface
class IamInterface
{
    virtual void myFunc() = 0;
}

希望这有助于下一个StackOverFlow的人逐步通过代码!