我的代码不会链接:“对Bumblebee :: Bumblebee()的“未定义”引用。

发布于 2025-01-27 23:51:39 字数 1707 浏览 2 评论 0原文

我正在尝试为此编码练习运行我的代码,并且我会遇到此错误:对bumblebee :: bumblebee(),对grasshopper :: grasshopper()

我正在练习继承,我将昆虫类用作基础类来继承功能和方法,但我不明白我在做什么错。感谢任何帮助,这里是代码:

#include <iostream>
using namespace std;

// Insect class declaration
class Insect
{
protected:
    int antennae;
    int legs;
    int eyes;

public:

    // default constructor
    Insect();       

    // getters
    int getAntennae() const;
    int getLegs() const;
    int getEyes() const;
};

// BumbleBee class declaration
class BumbleBee : public Insect
{
    public:
    BumbleBee();

    void sting()
    { cout << "STING!" << endl; }
};

// GrassHopper class declaration
class GrassHopper : public Insect
{
    public:
        GrassHopper();
        
        void hop()
        { cout << "HOP!" << endl; }
};


// main
int main()
{
    BumbleBee *bumblePtr = new BumbleBee;
    GrassHopper *hopperPtr = new GrassHopper;

    cout << "A bumble bee has " << bumblePtr->getLegs() << " legs and can ";
    bumblePtr->sting();

    cout << endl;

    cout << "A grass hopper has " << hopperPtr->getLegs() << " legs and can ";
    hopperPtr->hop();

    delete bumblePtr;
    bumblePtr = nullptr;
    delete hopperPtr;
    hopperPtr = nullptr;

    return 0;   
}

// member function definitions
Insect::Insect()
{
    antennae = 2;
    eyes = 2;
    legs = 6;
}
int Insect::getAntennae() const 
{ return antennae; }
int Insect::getLegs() const 
{ return legs; }
int Insect::getEyes() const 
{ return eyes; }


//Desired output
/*
A bumble bee has 6 legs and can sting!

A grass hopper has 6 legs and can hop!

*/

I am trying to run my code for this coding exercise and I am getting this error: undefined reference to BumbleBee::BumbleBee(), undefined reference to GrassHopper::GrassHopper()

I'm practicing inheritance and I am using the Insect class as a base class to Inherit functions and methods, but I don't understand what I am doing wrong. Any help is appreciated, here is the code:

#include <iostream>
using namespace std;

// Insect class declaration
class Insect
{
protected:
    int antennae;
    int legs;
    int eyes;

public:

    // default constructor
    Insect();       

    // getters
    int getAntennae() const;
    int getLegs() const;
    int getEyes() const;
};

// BumbleBee class declaration
class BumbleBee : public Insect
{
    public:
    BumbleBee();

    void sting()
    { cout << "STING!" << endl; }
};

// GrassHopper class declaration
class GrassHopper : public Insect
{
    public:
        GrassHopper();
        
        void hop()
        { cout << "HOP!" << endl; }
};


// main
int main()
{
    BumbleBee *bumblePtr = new BumbleBee;
    GrassHopper *hopperPtr = new GrassHopper;

    cout << "A bumble bee has " << bumblePtr->getLegs() << " legs and can ";
    bumblePtr->sting();

    cout << endl;

    cout << "A grass hopper has " << hopperPtr->getLegs() << " legs and can ";
    hopperPtr->hop();

    delete bumblePtr;
    bumblePtr = nullptr;
    delete hopperPtr;
    hopperPtr = nullptr;

    return 0;   
}

// member function definitions
Insect::Insect()
{
    antennae = 2;
    eyes = 2;
    legs = 6;
}
int Insect::getAntennae() const 
{ return antennae; }
int Insect::getLegs() const 
{ return legs; }
int Insect::getEyes() const 
{ return eyes; }


//Desired output
/*
A bumble bee has 6 legs and can sting!

A grass hopper has 6 legs and can hop!

*/

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

羁〃客ぐ 2025-02-03 23:51:39

您为基类提供了一个构造函数定义,但没有提供子类。您还需要从子类构造函数调用基类构造函数以获取所需的输出。
替换这样的子类构造函数:

BumbleBee() : Insect() {}

卷曲括号使其成为定义,而不是声明,并且在结肠后的初始化列表中调用基类构造函数。

You have provided a constructor definition for the base class but not the subclasses. You also will need to call the base class constructor from the subclass constructor to get your desired output.
Replace the subclass constructors like this:

BumbleBee() : Insect() {}

The curly braces make it a definition instead of a declaration, and the base class constructor gets called in the initialization list after the colon.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文