0

So I have three classes

class Map that stores objects of class Recty

class Map : public GameObject, public InCollisionMap
{
public:
    void Setup();
private:
    Recty *tile[24][32];  
};

class InCollisionMap //interface for map
{
    InCollisionRect *tile[24][32];
public:
    virtual InCollisionRect* Tile(int n, int m){
            int n = 0;
            int m = 0;
            return tile[n][m];  //returns object of class Recty
    }
    InCollision();
    ~InCollision();
};

class Recty that stores variable type

class Recty : public GameObject, public InCollisionRect
{
private:
    int type;
public:
    Recty();
    ~Recty();
};

class InCollisionRect //interface for class Recty
    {
    private:
       int type;
    public:
       virtual int get_number(char num);
       InCollisionRect();
       ~InCollisionRect();
};

and finally class GameObject

class GameObject : public InCollisionObject
{
public:
    GameObject(const char* textureSheet, SDL_Renderer* ren, int x, int y);
    GameObject();
    ~GameObject();
private:
    int xpos;
    int ypos;
    int way;

    SDL_Texture* objTexture;
    SDL_Rect srcRect, destRect;
    SDL_Renderer* renderer;
};

class InCollisionObject // interface for GameObject
{
    int xpos;
    int ypos;
    int way;

    SDL_Texture* objTexture;
    SDL_Rect srcRect, destRect;
    SDL_Renderer* renderer;
public:
    virtual void Collision_Loop(InCollisionMap* target){
          if (target->Tile(i, j)->get_number('t') == 0 {
                        ^
           /*{Tile(i,j) returns nullptr}*/
                  cout<<"NO<<endl;"
          }
    InCollisionObject();
    ~InCollisionObject();
};

and main

GameObject* player;
Map map;
int main(){
     map.Setup();
     player->Collision_Loop(&map);
 }

I kind of simplified the code but that every object in array *tile is initialized properly. Array is full of objects.

I am getting this error in Collision_Loop()

Exception thrown: read access violation.
InCollision::Tile[virtual](...) returned nullptr.

Why does it returns a nullptr? And how do I fix this?

No Name
  • 39
  • 1
  • 8
  • 4
    Without a [mcve] there's little that can be said about this. The random code fragments here, even though they're "simplified", fall far short from being a [mcve], and as such are of very little use. It's true that some very likely problems are apparent, and can be gleaned from these isolated code fragment (no, declaring two class members with the same name in the parent and the child class does ***not*** transform them into a single object), nothing authoritative can be answered here until the shown code complies with *all* requirements of a [mcve], as explained in stackoverlfow.com's [help]. – Sam Varshavchik May 27 '18 at 15:50
  • 2
    Note that you have a `tile` member in both the `InCollisionMap` and the `Map` classes. These will be two different things. Data members cannot be virtual. – Vaughn Cato May 27 '18 at 15:51
  • 3
    You have several variables with the same name. Add multiple inheritance to that, and you probably don't access the variable you expect, but something else with the same name. – Bo Persson May 27 '18 at 15:52
  • Thanks for the help. As you said the problem was bad inheritance. – No Name May 27 '18 at 16:44
  • We've got two people on this site called No Name. We're in trouble. – Paul Sanders May 28 '18 at 14:27

0 Answers0