【发布时间】:2015-03-24 08:50:06
【问题描述】:
我正在学习带有 C++ 的 OpenGL。我正在构建小行星游戏作为练习。我不太确定如何覆盖构造函数:
projectile.h
class projectile
{
protected:
float x;
float y;
public:
projectile();
projectile(float, float);
float get_x() const;
float get_y() const;
void move();
};
projectile.cpp
projectile::projectile()
{
x = 0.0f;
y = 0.0f;
}
projectile::projectile(float X, float Y)
{
x = X;
y = Y;
}
float projectile::get_x() const
{
return x;
}
float projectile::get_y() const
{
return y;
}
void projectile::move()
{
x += 0.5f;
y += 0.5f;
}
小行星.h
#include "projectile.h"
class asteroid : public projectile
{
float radius;
public:
asteroid();
asteroid(float X, float Y);
float get_radius();
};
main.cpp
#include <iostream>
#include "asteroid.h"
using namespace std;
int main()
{
asteroid a(1.0f, 2.0f);
cout << a.get_x() << endl;
cout << a.get_y() << endl;
}
我得到的错误:
main.cpp:(.text+0x20): undefined reference to `asteroid::asteroid(float, float)'
【问题讨论】:
-
你在哪里定义了小行星构造函数?
标签: c++ c++11 inheritance constructor compiler-errors