在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

当前回答

I believe most of the points regarding the causes and remedies have been covered by all contributors in this thread. I just want to point out for my 'unresolved external' problem, it was caused by a datatype defined as macro that gets substituted differently than expected, which results in that incorrect type being supplied to the function in question, and since the function with type is never defined, it couldn't have been resolved. In particular, under C/C++ -> Language, there is an attribute called 'Treat WChar_t As Built in Type, which should have been defined as 'No (/Zc:wchar_t-)' but did not in my case.

其他回答

我在我的项目中遇到了同样的错误,我已经设法解决了。

这个问题把我带到了这里,但我发现这是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。

有时,如果添加了一个新的头文件,并且由于这个错误开始出现,您还需要添加库来摆脱无法解析的外部符号。

例如:

#include WtsApi32.h

需要:

#pragma comment(lib, "Wtsapi32.lib") 

指针

我有这个问题,并解决了它使用指针。我知道这不是你的问题,但我想我应该提一下,因为我真希望一小时前看到这个的时候它就在这里。我的问题是关于声明一个静态成员变量而没有定义它(定义需要在一些其他设置之后),当然指针不需要定义。同样基本的错误:P

我有一个错误,我的项目被编译为x64项目。我使用的库是x86编译的。

我重新编译了x64库,它解决了这个问题。

确保你用

#ifndef YOUR_HEADER_H
#define YOUR_HEADER_H

// your header code here

#endif

如果你不这样做,就会发生不好的事情,包括这件事