在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

当前回答

我刚刚看到了一个问题,我不能在.cpp文件中调用main函数,在.h文件中正确声明,并在.c文件中定义。遇到一个链接错误。同时,我可以从通常的。c文件调用函数。这可能取决于调用约定。解决方案是在每个.h文件中添加以下预proc行:

#ifdef __cplusplus
extern "C"
{
#endif

这些在最后

#ifdef __cplusplus
}
#endif

其他回答

检查是否包含了您所引用的解决方案中的所有源文件。

如果你没有在项目中包含Field类的源文件(以及实现),它将不会被构建,你将无法在编译期间进行链接。

或者,也许您正在使用静态或动态库,并且忘记告诉链接器有关.lib的信息?

请参阅MSDN上的链接器工具错误LNK2019,它有导致LNK2019的常见问题的详细列表。

我的问题是一个sconscript没有定义在它的cpp文件。这可能非常令人困惑,因为Visual Studio在项目中有cpp文件,但正在构建的完全是其他东西。

只是花了几个小时才发现问题是我的主文件扩展名是。c而不是。cpp

:/

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

// 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”节点,其中源文件中的函数意外地嵌套在另一个名称空间下。