在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

当前回答

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

其他回答

我刚刚也犯了同样的错误,我设法通过替换来避免它;头文件中使用{}。

#ifndef XYZ_h
#define XYZ_h
class XYZ
{
    public:
    void xyzMethod(){}
}
#endif

当它是void xyzMethod();它不想编译。

指针

我有这个问题,并解决了它使用指针。我知道这不是你的问题,但我想我应该提一下,因为我真希望一小时前看到这个的时候它就在这里。我的问题是关于声明一个静态成员变量而没有定义它(定义需要在一些其他设置之后),当然指针不需要定义。同样基本的错误:P

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

例子:

// 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 */}

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

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