在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

当前回答

我也有同样的问题。在我取出最新的代码后,我的程序一天还能工作,第二天就不行了。

最新的代码不包括我在库中引用的项目。所以当我重建我的库时,它删除了。obj文件,whoopsy.....

我重新包含了我需要的项目,建立了我的库,然后重建了我的失败的项目,它运行得很好。

这个故事的寓意是,在深入到兔子洞之前,验证你的.obj文件是你引用它的地方。

其他回答

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.

我也有同样的问题。在我取出最新的代码后,我的程序一天还能工作,第二天就不行了。

最新的代码不包括我在库中引用的项目。所以当我重建我的库时,它删除了。obj文件,whoopsy.....

我重新包含了我需要的项目,建立了我的库,然后重建了我的失败的项目,它运行得很好。

这个故事的寓意是,在深入到兔子洞之前,验证你的.obj文件是你引用它的地方。

此错误通常意味着某些函数有声明,但没有定义。

例子:

// A.hpp
class A
{
public:
  void myFunc(); // Function declaration
};

// A.cpp

// Function definition
void A::myFunc()
{
  // do stuff
}

在你的情况下,找不到定义。问题可能是你包含了一个头文件,它带来了一些函数声明,但是你要么:

不要在CPP文件中定义函数(如果这段代码是您自己编写的) 不包括包含定义的lib/dll文件

一个常见的错误是你将一个函数定义为一个独立的函数,而忘记了类选择器,例如在你的.cpp文件中A::::

错误:void myFunc() {/* do stuff */} 右:void A::myFunc() {/* do stuff */}

此错误可能是由于将模板类的函数定义放在单独的.cpp文件中引起的。对于模板类,成员函数必须在头文件中声明。可以通过在.h文件中定义成员函数内联或紧跟类定义之后来解决这个问题。

例如,不像其他类那样将函数定义放在.cpp文件中,你可以像这样内联定义它们:

template<typename T>
MyClassName {
  void someFunction() {
    // Do something
    ...
  }
  void anotherFunction() {
    // Do something else
    ...
  }
}

或者你可以在类定义之后定义它们,但在同一个文件中,像这样:

template<typename T>
MyClassName {
  void someFunction();
  void anotherFunction();
}

void MyClassName::someFunction() {
  // Do something
  ...
}
void MyClassName::anotherFunction() {
  // Do something else
  ...
}

我只是想分享一下,因为似乎没有人提到模板类。这就是我的案例中出现错误的原因。

我在很长一段时间内第一次做一些c++,当我忘记为函数定义添加ClassName::前缀时,我得到了这个错误,因为这对c++来说有点独特。所以也要记得检查一下!