使用虚拟类时,方法返回nullptr

时间:2018-05-27 15:46:09

标签: c++ methods interface nullptr

所以我有三个班级

类存储类Recty

的对象的Map
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();
};

和主要

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

我简化了代码,但是array * tile中的每个对象都被正确初始化了。数组中充满了对象。

我在Collision_Loop()

中收到此错误
Exception thrown: read access violation.
InCollision::Tile[virtual](...) returned nullptr.

为什么会返回nullptr?我该如何解决这个问题?

0 个答案:

没有答案