0
\$\begingroup\$

i made a small code to debug and i get this error

player inherit from gameobject

code

///main
#include "game.h" 

 gameobject* pla;

int main()
{

    std::vector<gameobject*> objects;

    gameobject* pla = new player(32,32);  -----> error is here

    objects.push_back(pla);
    cout<<objects[0]->numberID<<endl;
    return 0;
}


/// gameobject
pragma once

#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include "game.h"


class gameobject
{

    public:

        virtual ~gameobject(){};
        virtual void init()=0;
        virtual void tick()=0;
        virtual void render(SDL_Renderer* ren)=0;
        virtual void destroy()=0;
        virtual void setVelx(int velx)=0;
        virtual void setVely(int vely)=0;



        int numberID=0;


    protected:

        int width = pos.w;
        int height = pos.h;
        int posx = pos.x;
        int posy = pos.y;

        float velox;
        float veloy;

        SDL_Rect crop;
        SDL_Rect pos;

        SDL_Texture* tex;


////player.h

#pragma once

#include "gameobject.h"



class gameobject;

class player : public gameobject
{


public:

    void init() override;
    void tick() override;
    void render(SDL_Renderer* ren) override;
   // SDL_Renderer* renderer;
    player(int pwidth,int pheight);

    void destroy() override{
        this->~player();
    }

    void setVelx(int velx) override;
    void setVely(int vely) override;

    int i = 0;

private:

    ~player() override;




};


/// player.cpp


#include "player.h"


player::player(int pwidth, int pheight)
{

    this->pos.w = pwidth; //width
    this->pos.h = pheight; // height
    this->pos.x = 100; //posx
    this->pos.y = 100; //posy


    // cout<<"yes iam ren : "<<ren<<endl;


    this->numberID = gameobject::numberID;

    gameobject::numberID++;
    //this->tex = IMG_LoadTexture(ren,"/home/younlok/Desktop/dash/res/pss.png");
    this->tex = game::textures[0];
    crop.x = 0;
    crop.y = 0;
    crop.w = 32;
    crop.h = 32;

    //game::hand->addObject(this);

}

void player::init()
{
}

void player::tick()
{

    this->pos.x += this->velox;
    this->pos.y += this->veloy;

}



void player::render(SDL_Renderer* ren)
{

 SDL_RenderCopy(ren,tex,&crop,&pos);

}

void player::setVelx(int velx)
{
    this->velox = velx;
}

void player::setVely(int vely)
{
    this->veloy = vely;
}

player::~player(){}







\$\endgroup\$
  • \$\begingroup\$ Does the compiler complains in any way? Did you enable all of the warnings? \$\endgroup\$ – Vaillancourt Dec 3 '19 at 17:23
  • \$\begingroup\$ explain more sir pls what do you mean ?? this is the only error i get \$\endgroup\$ – younlok Dec 3 '19 at 17:30
  • \$\begingroup\$ You get an error at runtime. Your compiler would also issue errors if the program was not correctly coded. Compilers also issue warnings when some things are correctly coded but could cause issues. I don't know what compiler you use but they generally allow multiple levels of "warnings" via command line arguments. You should make sure you compile with the highest level of warning to avoid nasty behaviour. \$\endgroup\$ – Vaillancourt Dec 3 '19 at 17:36
  • \$\begingroup\$ Also, prefer gender-neutral language when uncertain, as stated in the Code of Conduct. \$\endgroup\$ – Vaillancourt Dec 3 '19 at 17:40
  • \$\begingroup\$ sorry i just get warnings about converting strings and stuff the problem is that a prototype doesn't work but in the actual project the game it works \$\endgroup\$ – younlok Dec 3 '19 at 17:43
0
\$\begingroup\$

What is a segmentation fault?

Core Dump/Segmentation fault is a specific kind of error caused by accessing memory that “does not belong to you.”

(Source).

Huh? What? Where and how am I doing this?? During the construction of your object (not exactly in the constructor).

— Then, nonstatic data members shall be initialized in the order they were declared in the class definition (again regardless of the order of the mem-initializers).

(Source)

Let's look at your gameobject class.

class gameobject
{
    ...

    protected:

        int width = pos.w;
        int height = pos.h;
        int posx = pos.x;
        int posy = pos.y;
        ... 
        SDL_Rect pos;

You're trying to use pos before it's been initialized.

Don't do that.

Here are a couple of suggestions:

  • default-initialize your values with literals (i.e. using explicit default values) like 0 and not with another object in your class (I think that's kind of weird and risky).
  • define a code standard that if the values are not default-initialized in the header, that's because they're initialized in the constructor.
  • (this one my favourit) don't even use those four variables, because you already have a SDL_Rect pos; that has the information and that's against the DRY principle.

As a side note, this is weird and makes me suspect a design flaw:

    void destroy() override{
        this->~player();
    }

This implies that the destroy() method is the last one called before the object is released.

Why do you need a destroy method when you could just call delete myPlayer;?

Which then suggests you should take a look into smart pointers to avoid all of this memory management.

\$\endgroup\$
  • \$\begingroup\$ your answer still didn't solve it also about the destroy well in my game i have handler which loop over all the objects in my game and delete them and iam having an error with that see (maybe you can help me ): [gamedev.stackexchange.com/questions/177444/… i still get the same error \$\endgroup\$ – younlok Dec 3 '19 at 17:06
  • \$\begingroup\$ Can you reproduce the issue here? \$\endgroup\$ – Vaillancourt Dec 3 '19 at 17:11
  • \$\begingroup\$ coliru.stacked-crooked.com/a/ade3c7f4d7f2bb04 i don't get error here but when i run it on my pc it does and when i run the whole project (the actual game) it works but with this small prototype it doesn't i think it worked in that website because there is one file \$\endgroup\$ – younlok Dec 3 '19 at 17:41
  • \$\begingroup\$ You do get errors, the program does not compile on coliru. \$\endgroup\$ – Vaillancourt Dec 3 '19 at 17:51
  • \$\begingroup\$ i don't get them lol but does that help you to figure it out \$\endgroup\$ – younlok Dec 3 '19 at 17:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.