【问题标题】:Struggling with error message undefined reference to `Fish::Fish(std::string)'挣扎于错误消息未定义对“Fish::Fish(std::string)”的引用
【发布时间】:2021-10-28 03:38:03
【问题描述】:

我有这个 UML 图,我已经编写了下面的代码,但我正在努力处理错误消息

但是,在编译和链接时,我得到了一个错误

/tmp/cc9oQaPX.o: In function `main':
main.cpp:(.text+0x8c): undefined reference to `Fish::Fish(std::string)'
main.cpp:(.text+0xe5): undefined reference to `Cat::Cat(std::string)'
main.cpp:(.text+0x116): undefined reference to `Fish::Fish()'
main.cpp:(.text+0x158): undefined reference to `Cat::Cat()'
collect2: error: ld returned 1 exit status
#include <iostream>
using namespace std;
class Animal  // define base class 
{
protected:
    int legs;        // base class properties
public:
    Animal(int legNumbers)    // set values of leg
    {
        legNumbers = legs;   // set values of leg 
    }
    virtual void eat() = 0;  // method of base class
    virtual void walk() {}; // method of base class


};
class Pet      // define the pet class 
{
protected:
    string name;       // set properties of pet class 
public: 
    virtual string getName();       // define method
    virtual string setName(string name);    // set name values 
    virtual void play()          // define play method 
    {
        cout << " garfield is playing now." << endl;  // out values 
    }
};
class Spider :public Animal   //child class inherit base class
{
public:
    Spider() :Animal(8)   // spider class inherit animal class
    {
        cout << "animals with " << legs << " legs is walking. " << endl;
    }
    virtual void eat()   // define virtual method
    {
        cout << "spider is eating now. " << endl;
    }
};
class Cat :public Pet, public Animal  // cat inherit two classes 
{
public:
    Cat(string name);   // set name method
    Cat();       
    virtual void play()   // define method
    { 
        cout << name << " is playing now. " << endl;
    }
    virtual void eat();     // define method here

};
class Fish : public  Pet, public Animal  // fish inherit two method
{
public:        // define public members
    Fish(string name);
    Fish();
    virtual void play()
    {
        cout << name << " is playing now. " << endl;
    }
    virtual void eat();   // method here 
    void walk() 
    {
        cout << " Fish cannot walk " << endl;  // output the values 
    }
};

string Pet::getName()   // get name value from parent class
{
    return string();
}

string Pet::setName(string name)
{
    return string();
}
int main(int argc, char* argv[])   // define main method 
{
    Fish* f = new Fish("Jaws");
    Cat* c = new Cat("Tenkir");
    Animal *a = new Fish();
    Animal* e = new Spider();
    Pet* p = new Cat();
    f->play();
    c->play();
    e->eat();
    e->walk();
    a->walk();
    p->play();
    return 0;
}

【问题讨论】:

  • 您对构造函数有未定义的引用。您需要定义它们。
  • @cigien 请检查我的代码并告诉我如何解决此错误消息?
  • 例如,您声明了Cat(string name);,但从未定义它。你需要这样做,仅此而已。
  • @cigien 非常感谢您回答我这个问题,非常感谢您..
  • 糟糕,这当然是重复的问题:stackoverflow.com/questions/68963268/… 所以请不要以多个用户的身份发布问题,尤其是当他们关闭时。

标签: c++ c++11 visual-c++ c++14


【解决方案1】:

这是正确的代码。

首先,您没有定义 Cat an Fish 的构造函数 但这不是唯一的问题。 你的功能走路,吃饭,玩耍需要被覆盖。而且eat() 必须有一个块,因为在基类(Animal) 中被删除了,比如eat() = 0;

#include <iostream>
using namespace std;
class Animal  // define base class 
{
protected:
    int legs;        // base class properties
public:
    Animal(int legNumbers)    // set values of leg
    {
        legs = legNumbers;   // set values of leg 
    }
    virtual ~Animal() {}
    virtual void eat() = 0;  // method of base class
    virtual void walk() {}; // method of base class


};
class Pet      // define the pet class 
{
protected:
    string name;       // set properties of pet class 
public: 
    virtual string getName();       // define method
    virtual void setName(string name);    // set name values 
    virtual void play()          // define play method 
    {
        cout << " garfield is playing now." << endl;  // out values 
    }
};

string Pet::getName()   // get name value from parent class
{
    return this->name;
}

void Pet::setName(string name)
{
    this->name = name;
}

class Spider :public Animal   //child class inherit base class
{
public:
    Spider() :Animal(8)   // spider class inherit animal class
    {
        cout << "animals with " << legs << " legs is walking. " << endl;
    }
    virtual void eat()   // define virtual method
    {
        cout << "spider is eating now. " << endl;
    }
};
class Cat :public Pet, public Animal  // cat inherit two classes 
{
public:
    Cat(string name)
    :Animal(4)
    {
        this->setName(name);
    }   // set name method
    Cat():Animal(4){}     
    ~Cat() override {} 
    virtual void play() override   // define method
    { 
        cout << name << " is playing now. " << endl;
    }
    virtual void eat() override {}     // define method here

};
class Fish : public  Pet, public Animal  // fish inherit two method
{
public:        // define public members
    Fish(string name)
    :Animal(0)
    {
        this->setName(name);
    }
    Fish():Animal(0){}
    ~Fish() override {}
    virtual void play() override
    {
        cout << name << " is playing now. " << endl;
    }
    virtual void eat() override {}  // method here 
    void walk() override
    {
        cout << " Fish cannot walk " << endl;  // output the values 
    }
};


int main(int argc, char* argv[])   // define main method 
{
    Fish* f = new Fish("Jaws");
    Cat* c = new Cat("Tenkir");
    Animal *a = new Fish();
    Animal* e = new Spider();
    Pet* p = new Cat();
    f->play();
    c->play();
    e->eat();
    e->walk();
    a->walk();
    p->play();
    return 0;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-10-30
    • 2012-09-28
    • 1970-01-01
    • 2014-10-18
    • 2023-01-07
    • 1970-01-01
    • 1970-01-01
    • 2023-04-06
    相关资源
    最近更新 更多