在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

当前回答

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

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

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

其他回答

导致此链接器错误的一个可能原因还可能是在头文件中声明但没有定义的内联函数,该头文件随后包含在其他地方。内联函数必须在使用它们的每个翻译单元中定义。

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

#ifdef __cplusplus
extern "C"
{
#endif

这些在最后

#ifdef __cplusplus
}
#endif

还有一个可能的问题(我只是抓了一段时间):

如果将函数定义为内联的,那么它们——当然!-必须在头文件(或内联文件)中定义,而不是在cpp中定义。 在我的例子中,它们在一个内联文件中,但这只是因为它们是一个特定于平台的实现,并且cpp包含了这个相应的内联文件……而不是一个头文件。是啊,真他妈倒霉。

我想我应该把这个也放在这里,也许其他人遇到了同样的问题,并在这里找到了它。

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.

我的问题是:我必须对一个“外部未解决”的课程做前向声明。

在我得到错误的文件中,我必须放这样的东西:

#include "ClassB" 

class ClassB; // this solved the problem

class ClassA{
    void foo(){
        ClassB* tmp = new ClassB();
        // ...
    }
};

当然,我的项目要复杂得多,这只是一个片段示例。同样,在使用名称空间时,也要声明它们。