-2

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!

*/

user207421
  • 305,947
  • 44
  • 307
  • 483
Keith Coye
  • 75
  • 1
  • 7
  • 2
    `BumbleBee();` is a declaration, a promise that the function will exist, but not here. You don't seem to have kept this promise. – user4581301 May 12 '22 at 01:15
  • 1
    A useful ploy is to argue with your compiler. Your compiler is telling you that `BumbleBee::BumbleBee()` does not exist. Can you point to the definition of `BumbleBee::BumbleBee()`, thus demonstrating that your compiler is wrong? *(If you can point to it, that tells us that we should explain why that thing is not a definition, rather than resorting to telling you the same thing your compiler did -- that the definition is missing.)* – JaMiT May 12 '22 at 01:31
  • Your `Insect` class lacks a virtual destructor. Thus this: `Insect *ptr = new BumbleBee(); delete ptr;` invokes undefined behavior. Please add to `Insect`: `virtual ~Insect() = default;` – PaulMcKenzie May 12 '22 at 01:44

1 Answers1

0

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.

Blake
  • 1
  • 1
  • 1
  • Since `insect` offers a default constructor and there are no complicated member variables that need extra help, you can simplify a step further in a modern C++ compiler: `BumbleBee() = default;` – user4581301 May 12 '22 at 01:37
  • You don't need to explicitly call a default constructor such as `Insect()`. The compiler will insert that automatically. – user207421 May 12 '22 at 03:40