【问题标题】:c++ error: :-1: error: symbol(s) not found for architecture x86_64 - in Qt-Creatorc++错误::-1:错误:未找到架构x86_64的符号-在Qt-Creator中
【发布时间】:2016-11-29 15:54:52
【问题描述】:

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

演员.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(actor 的子类):

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;
};

平面.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;
}

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

【问题讨论】:

标签: c++ qt qt-creator


【解决方案1】:

您已经在actor.h 中声明了一个类Actor

class Actor {
    public: Actor();
};

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

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

现在您已经声明了 Plane,它是 Actor 的子类:

class Plane : Actor {
};

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

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

由于PlaneActor 的子类,默认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;
};

现在,至于这里的行为 - movepos_xpos_y 方法均未声明为 virtual,因此它们不会在 Plane 中被重载;他们只是被替换。这可能会在您的课程后期出现。

【讨论】:

  • 那我应该写一个actor.cpp文件吗?
  • 如果你打算实现正确的构造函数,并且有movepos_xpos_y的实现,那么添加一个actor.cpp文件将是一种方法;但是,正如我已经在答案中评论过的那样;因为代码很简单,.h 文件中的实现完全可以接受。
  • 确保您没有在actor.h 文件中写入Actor::Actor() {},而只是在文件中写入Actor() {} - 这就是extra qualification 警告的原因(我可以' t 编辑我的评论以替换错误书写的文本)。
猜你喜欢
  • 2015-05-31
  • 1970-01-01
  • 2018-11-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-27
  • 2019-02-21
  • 1970-01-01
相关资源
最近更新 更多