c++ error: :-1: error: symbol(s) not found for architecture x86_64 - in Qt-Creator

c++ error: :-1: error: symbol(s) not found for architecture x86_64 - in Qt-Creator

我正在大学做一个练习,每次我尝试编译 main.cpp 我总是得到同样的错误。

actor.h:

class Actor {
public:
Actor();
Actor(double x0, double y0);
void move();
double pos_x();
double pos_y();

static const int ARENA_W = 500;
static const int ARENA_H = 500;
};

plane.h(演员的子类):

class Plane:Actor
{
public:
Plane();
Plane(double x0, double y0);
void move();
double pos_x();
double pos_y();

//int dx = 5;
static const int W = 50;
static const int H = 20;

private:
double x, y;
};

plane.cpp

#include "plane.h"
#include "actor.h"

Plane::Plane(double x0, double y0)
{
this ->x = x0;
this ->y = y0;
//this -> dx;
}

void Plane::move()
{
x = x + 2.5 ;
}

 double Plane::pos_x()
{
return x;
}
double Plane::pos_y()
{
return y;
}

main.cpp

include "plane.h"
include"actor.h"

using namespace std;

int main(int argc, char *argv[])
{
Plane plane1(25.0, 5.0);
plane1.move();
double x = plane1.pos_x();
double y = plane1.pos_y();
cout << x << " , " << y<<endl;
}

我看到有很多关于这个问题的问题,但我没有解决它。 你能帮我吗()? 谢谢

您在 actor.h 中声明了一个 class Actor:

class Actor {
    public: Actor();
};

这意味着您将要编写一些代码来定义此构造。这通常会在 Actor.cpp 文件中结束。

如果您在没有此实现的情况下尝试构建 Actor,您将从链接器收到错误,因为您缺少默认构造函数。

现在您已经声明了 PlaneActor 的子class:

class Plane : Actor {
};

并且您已经定义了一个非默认构造函数:

Plane::Plane(double, double) {
   // does something
}

因为 PlaneActor 的子 class,所以有一个默认 Actor 的隐式构造作为 Plane 构造的一部分,并且当您声明将有一个实现时,链接器正在期待它。由于您从未在代码中定义它,因此此时链接器失败。

有点简单的解决方案是在 actor.h 中添加一个简单的构造函数;即:

class Actor {
public:
Actor() {} // replace the ; with {}
Actor(double x0, double y0);
void move();
double pos_x();
double pos_y();

static const int ARENA_W = 500;
static const int ARENA_H = 500;
};

Now, as for behaviours here - none of the move, pos_x or pos_y methods are declared virtual, so they're not being overloaded in Plane; they're simply being replaced. This may come up later in your course.