得到错误LNK2019和LNK1120,无法弄清楚我做错了什么

时间:2014-04-11 16:02:24

标签: c++ lnk2019

我收到了这些错误:

Error 1   error LNK2019: unresolved external symbol "public: __thiscall
Animal::Animal(int,class std::basic_string<char,struct std::char_traits<char>,class
std::allocator<char> >)" (??0Animal@@QAE@HV?$basic_string@DU?$char_traits@D@std@@V?
$allocator@D@2@@std@@@Z) referenced in function _wmain

Error 2   error LNK1120: 1 unresolved externals

我试图谷歌寻求答案,但我似乎无法让它发挥作用......

// ConsoleApplication3.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <string>
#include <conio.h>
using namespace std;

class Animal
{
protected:
    int age;
    string type;
public:
    void virtual sleep() { cout << type << " : Sleeping" << endl; }
    void virtual eat() { cout << type << " : Eating" << endl; }
    int getAge() { return age; }
    string getType() { return type; }
    Animal(int argAge) : age(argAge) {}
    Animal() : age(0) {}
    Animal::Animal(int, string);
};

class Lion : public Animal
{
public:
    void sleep() { cout << "The lion is sleeping" << endl; }

};

class Hamster : public Animal
{
public:
    void eat() { cout << "The hamster is eating" << endl; }
};

char YorN;
string aniType;
int eatOrSleep = 0;
int check = 0;
int _tmain(int argc, _TCHAR* argv[])
{
    Lion scar;
    scar.eat();

    Hamster hammertime;
    hammertime.sleep();

    cout << "Would you like to create a new animal? y/n" << endl;
    cin >> YorN;
    if (YorN == 'y' || YorN == 'Y'){
        cout << "What kind of animal?:" << endl;
        cin >> aniType;
        Animal newAnimal(0, aniType);
        cout << "Congratualtions, you just created a" << newAnimal.getType() << endl;
        do
        {
            cout << "Enter either 1, 2 or 3:" << endl <<
                "1: Makes your animal sleep" << endl <<
                "2: Makes your animal eat" << endl <<
                "3: Exit the program" << endl;
            cin >> eatOrSleep;
            if (eatOrSleep == 1)
            {
                newAnimal.sleep();
            }
            else if (eatOrSleep == 2)
            {
                newAnimal.eat();
            }
            else if (eatOrSleep == 3)
            {
                check = 1;
                break;
            }
        } while (check == 0);

    }

    return 0;
}

任何帮助都将不胜感激。

3 个答案:

答案 0 :(得分:2)

让我们尝试简化原始的长错误消息,减少一些&#34;噪音&#34;,以使错误消息更容易理解。

原始错误消息为:

Error 1 error LNK2019: unresolved external symbol 
"public: __thiscall Animal::Animal(int, class std::basic_string<char,
struct std::char_traits<char>,class std::allocator<char> >)" 
(??0Animal@@QAE@HV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z) 
referenced in function _wmain

首先,可以丢弃包含??0Animal@@QAE...等的部分,因为这只是C ++ name mangling ,并且它不是人类可读的。

因此,在第一步之后,简化的错误消息变为:

Error 1 error LNK2019: unresolved external symbol "public: __thiscall Animal::Animal(int,
class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)"
referenced in function _wmain

没有C ++名称修改的这个版本更清楚。 链接器抱怨某些缺少的方法定义(并且该方法在_wmain()函数内引用)。

让我们尝试进一步简化错误消息,过滤掉更多&#34;噪音&#34;:

  1. __thiscall是C ++类方法使用的调用约定。

  2. 部分:

    class std::basic_string<char,struct std::char_traits<char>,
      class std::allocator<char>>
    

    只是STL std::string的长名称(std::basic_stringchar作为字符类型,并使用标准分配器。

  3. 因此,错误消息可以进一步简化为:

    Error 1 error LNK2019: unresolved external symbol "public: Animal::Animal(int, string)" 
    referenced in function _wmain
    

    这很清楚:链接器抱怨Animal形式的Animal::Animal(int, string)构造函数缺少定义。

    为该构造函数提供适当的主体,例如:

    class Animal {
    ....
    public:
    
        Animal(int anAge, string aType)
            : age(anAge),
            , type(std::move(aType))  // C++11: pass by value and move from the value
        { }
    ....
    

答案 1 :(得分:1)

Animal::Animal(int, string);

您需要定义此功能的主体。也许这就是你想要的

Animal::Animal(int _age, string _type)
{
    age = _age;
    type = _type;
}

答案 2 :(得分:1)

首先,这个带有两个参数的构造函数声明

Animal::Animal(int, string);
虽然MS VC ++编译代码,但

无效。

您必须在不使用限定名称

的情况下声明它
Animal(int, string);

至于错误消息,那么你忘了定义构造函数,但在语句

中调用它
Animal newAnimal(0, aniType);