在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

当前回答

我遇到了类似的问题,最终通过在类的声明中添加__declspec(dllimport)来解决它:

// A.hpp
class __declspec(dllimport) A
{
   public: void myFunc();

   // Function declaration
};

其他回答

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

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

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

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

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

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.

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

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

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

除了上面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的人逐步通过代码!