0

This one should be an easy fix, or so I thought.

Error

Segmentation error while initializing my world.

A function is called to move a Vehicle, this checks if the location it would move to is valid, and there I need to reach back to the World to see its boundaries. This pointer to the World is not initialised correctly. However, in the debugging window, the world pointer is shown to be valid.

Note: The world pointer in the debugger shows an initialised world (so it's not randomly constructing an empty one).

! Look at this image: Debugger shows valid pointer, however, it still errors. I am very confused.

Relevant (?) Code

Function called:

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 Player:

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 Vehicle:

    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]);
}
Yunnosch
  • 26,130
  • 9
  • 42
  • 54
YarrikV
  • 1
  • 2
  • 1
    Not sure if it is the cause of your problem, but your `Vehicle::Vehicle(World * w)` constructor does not fill in any of the fields. It creates a temporary `Vehicle` and then throws it away. – Botje Jun 25 '21 at 11:22
  • Also, your `Player::world` shadows the `Vehicle::world` it inherits, so that can cause problems as well. Please edit your post into a [mcve] we can test in order to help you. – Botje Jun 25 '21 at 11:24
  • you forgot to new world object , in line you write `World * world; ` replace it to `World *world = new World();` – Parisa.H.R Jun 25 '21 at 11:49
  • What Botje said in his first comment fixed this issue. I thought i could use the other constructor of my Vehicle to not have to repeat everything... It feels to wrong to have to copy the content of the other constructor in the constructor with only world as parameter. – YarrikV Jun 25 '21 at 12:53
  • Please do not edit solution announcements into the question. Accept (i.e. click the "tick" next to it) one of the existing answer, if there are any. You can also create your own answer, and even accept it, if your solution is not yet covered by an existing answer. – Yunnosch Jun 25 '21 at 12:56
  • *I thought i could use the other constructor of my Vehicle to not have to repeat everything* You can, you just need to learn the right syntax. – n. m. could be an AI Jun 25 '21 at 13:28

1 Answers1

0

Solution was said in the comments by Botje: " Not sure if it is the cause of your problem, but your Vehicle::Vehicle(World * w) constructor does not fill in any of the fields. It creates a temporary Vehicle and then throws it away."

I just copied the content of the other constructor to actually construct the object. This is not a very nice solution, i think. Since I have to copy code. If this can be avoided somehow, please tell me how. (I first tried doing this = Vehicle(other constructor), but a constructor does not return anything.)

EDIT: It is called delegating constructors, or at least, I found the right way to do it in this question.

YarrikV
  • 1
  • 2