在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

当前回答

这是另一种检查的可能性,这次是我的问题。

我已经将函数添加到库中,并将库的输出文件夹包含在搜索路径中。

但是我也有一个文件夹,里面有之前列出的旧版本的库,所以VS使用的是旧的库,当然没有找到新函数。

其他回答

在我的例子中,我需要将函数名添加到DEF文件中。

LIBRARY   DEMO
EXPORTS
   ExistingFunction   @1
   MyNewFunction      @2

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

我只是一时难以接受。一切都是有逻辑的。我声明了一个构造函数,但没有定义它

class SomeClass
{
   SomeClass();  // needs the SomeClass::SomeClass(){} function defined somewhere, even here
}

当我忘记这么基本的东西时,我的头差点撞到键盘上。

导致“未解析的外部符号”错误的一个可能原因是函数调用约定。

确保所有源文件都使用相同的标准(.c或.cpp),或者指定调用约定。

否则,如果一个文件是C文件(source.c),另一个文件是.cpp文件,并且它们链接到相同的头文件,那么将抛出“未解析的外部符号”错误,因为该函数首先被定义为C cdecl函数,但随后使用相同头文件的c++文件将寻找c++函数。

为了避免“未解析的外部符号错误”,请确保函数调用约定在使用它的文件中保持相同。

是什么导致了我的这种情况:

我有一个很大的文件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;
}