我的代码不会链接:“对Bumblebee :: Bumblebee()的“未定义”引用。
我正在尝试为此编码练习运行我的代码,并且我会遇到此错误:对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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您为基类提供了一个构造函数定义,但没有提供子类。您还需要从子类构造函数调用基类构造函数以获取所需的输出。
替换这样的子类构造函数:
卷曲括号使其成为定义,而不是声明,并且在结肠后的初始化列表中调用基类构造函数。
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:
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.