Метод возвращает nullptr при использовании виртуального класса - PullRequest
0 голосов
/ 27 мая 2018

Итак, у меня есть три класса

класса Map, в которых хранятся объекты класса 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();
};

класса Recty, в которых хранятся переменные типа

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

и, наконец, класс 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();
};

и main

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

Я немного упростил код, но каждый объект массива * правильно инициализирован.Массив полон объектов.

Я получаю эту ошибку в Collision_Loop ()

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

Почему возвращается nullptr?И как мне это исправить?

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...