调试器显示名称空间没有问题时出现意外分段错误
Unexpected Segmentation fault while debugger shows namespace without issues
这个应该很容易解决,我是这么认为的。
错误
初始化我的世界时出现分段错误。
调用一个函数来移动 Vehicle,这将检查它要移动到的位置是否有效,并且我需要返回到 World 以查看其边界。这个指向世界的指针没有正确初始化。但是在调试中window,显示world指针是有效的
注意:调试器中的世界指针显示了一个初始化的世界(因此它不是随机构建一个空的世界)。
! Look at this image: Debugger shows valid pointer, however, it still errors. I am very confused.
相关(?)代码
函数调用:
void World::vehicleMoverTimerPing()
{ // Function every time short timer timeouts
// move player
if (player->speed != 0) {
player->move(game->TIMER_SHORT_DURATION);
}
// add to age (in seconds)
age += (float)game->TIMER_SHORT_DURATION/1000;
}
Class 玩家:
class Player : public Vehicle
{
private:
World * world;
public:
Player(World*);
};
Player::Player(World * w) : Vehicle(w)
{
// add to world & game
world = w;
world->player = this;
}
Class 车辆:
class Vehicle : public QObject, public QGraphicsPixmapItem {
private:
World * world;
// PLAYER CANNOT ONLY ACCESS THESE VARIABLES THROUGH FUNCTIONS OF VEHICLE
// driving variables
float orientation_degree;
bool location_is_valid(QPointF); // IN GRID COORDS
QPointF direction(); // orientation
public:
// type & constructor
Vehicle_type * type;
QPointF location;
float speed;
Vehicle(World*, QPointF, Vehicle_type*, QObject *parent=nullptr);
Vehicle(World*);
World* getWorld() {return world;}
... unrelated
}
Vehicle::Vehicle(World* w, QPointF spawn_position, Vehicle_type* t, QObject *parent) : QObject(parent)
{
world = w;
speed = 0;
type = t;
// spawn at center of the tile at position spawn
location = spawn_position+QPointF(0.5 - t->dimensions.x()/2, 0.5 - t->dimensions.y()/2);
orientation_degree = 0;
turn(orientation_degree);
}
Vehicle::Vehicle(World * w)
{
Vehicle(w, w->viewportCenter, w->getGame()->vehicle_types[0]);
}
Botje 在评论中提到了解决方案:
“不确定这是否是您问题的原因,但您的 Vehicle::Vehicle(World * w)
构造函数未填写任何字段。它创建一个临时 Vehicle
然后将其丢弃。”
我只是复制了另一个构造函数的内容来实际构造对象。
我认为这不是一个很好的解决方案。因为我必须复制代码。如果可以以某种方式避免这种情况,请告诉我如何避免。
(我首先尝试做 this = Vehicle(other constructor)
,但构造函数没有 return 任何东西。)
编辑:它被称为 delegating constructors, or at least, I found the right way to do it in this question。
这个应该很容易解决,我是这么认为的。
错误
初始化我的世界时出现分段错误。
调用一个函数来移动 Vehicle,这将检查它要移动到的位置是否有效,并且我需要返回到 World 以查看其边界。这个指向世界的指针没有正确初始化。但是在调试中window,显示world指针是有效的
注意:调试器中的世界指针显示了一个初始化的世界(因此它不是随机构建一个空的世界)。
! Look at this image: Debugger shows valid pointer, however, it still errors. I am very confused.
相关(?)代码
函数调用:
void World::vehicleMoverTimerPing()
{ // Function every time short timer timeouts
// move player
if (player->speed != 0) {
player->move(game->TIMER_SHORT_DURATION);
}
// add to age (in seconds)
age += (float)game->TIMER_SHORT_DURATION/1000;
}
Class 玩家:
class Player : public Vehicle
{
private:
World * world;
public:
Player(World*);
};
Player::Player(World * w) : Vehicle(w)
{
// add to world & game
world = w;
world->player = this;
}
Class 车辆:
class Vehicle : public QObject, public QGraphicsPixmapItem {
private:
World * world;
// PLAYER CANNOT ONLY ACCESS THESE VARIABLES THROUGH FUNCTIONS OF VEHICLE
// driving variables
float orientation_degree;
bool location_is_valid(QPointF); // IN GRID COORDS
QPointF direction(); // orientation
public:
// type & constructor
Vehicle_type * type;
QPointF location;
float speed;
Vehicle(World*, QPointF, Vehicle_type*, QObject *parent=nullptr);
Vehicle(World*);
World* getWorld() {return world;}
... unrelated
}
Vehicle::Vehicle(World* w, QPointF spawn_position, Vehicle_type* t, QObject *parent) : QObject(parent)
{
world = w;
speed = 0;
type = t;
// spawn at center of the tile at position spawn
location = spawn_position+QPointF(0.5 - t->dimensions.x()/2, 0.5 - t->dimensions.y()/2);
orientation_degree = 0;
turn(orientation_degree);
}
Vehicle::Vehicle(World * w)
{
Vehicle(w, w->viewportCenter, w->getGame()->vehicle_types[0]);
}
Botje 在评论中提到了解决方案:
“不确定这是否是您问题的原因,但您的 Vehicle::Vehicle(World * w)
构造函数未填写任何字段。它创建一个临时 Vehicle
然后将其丢弃。”
我只是复制了另一个构造函数的内容来实际构造对象。
我认为这不是一个很好的解决方案。因为我必须复制代码。如果可以以某种方式避免这种情况,请告诉我如何避免。
(我首先尝试做 this = Vehicle(other constructor)
,但构造函数没有 return 任何东西。)
编辑:它被称为 delegating constructors, or at least, I found the right way to do it in this question。