0

由于上述错误,我无法修改类对象的成员值:

当我调用我的初始化函数来初始化我的“garo”对象时,我收到以下运行时错误,

“Heretic.exe 中 0x01323976 处的未处理异常:0xC0000005:访问冲突读取位置 0x00000008。”

注意:Garo 类是 Object 的子类。

编码

加罗

#pragma once
#include "Object.h"

class Garo : public Object
{
private:
    int animationRow;

public:
    Garo();
    void Destroy();

    void Init(ALLEGRO_BITMAP *image = NULL);
    void Update();
    void Render();

    void MoveLeft();
    void MoveRight();
    void Idle();
    void SetAnimationRow(int row);
};

加罗.cpp

#include "Garo.h"

Garo::Garo()
{
    Object::Init(20, 200, 3, 0, 0, 0, 16, 24);
}

void Garo::Init(ALLEGRO_BITMAP *image)
{
    Object::Init(20, 200, 3, 0, 0, 0, 16, 24);

    SetID(PLAYER);
    SetAlive(true);

    maxFrame = 3;
    curFrame = 0;
    frameWidth = 32;
    frameHeight = 48;
    animationColumns = 4;
    animationDirection = 1;

    animationRow = 0;

    if(image != NULL)
        Garo::image = image;
}

...其余的已缩写

对象.h

#pragma once

#include <iostream>
#include <allegro5/allegro5.h>
#include <allegro5/allegro_primitives.h>
#include "Globals.h"

class Object
{
private:
    int ID;
    bool alive;
    bool collidable;

protected:
    float x;
    float y;

    float velX;
    float velY;

    int dirX;
    int dirY;

    int boundX;
    int boundY;

    int maxFrame;
    int curFrame;
    int frameCount;
    int frameDelay;
    int frameWidth;
    int frameHeight;
    int animationColumns;
    int animationDirection;

    ALLEGRO_BITMAP *image;

public:
    Object();
    void virtual Destroy();

    void Init(float x, float y, float velX, float velY, int dirX, int dirY, int boundX,               int boundY);
    void virtual Update();
    void virtual Render();

    float GetX() {return x;}
    float GetY() {return y;}

    void SetX(float x) {Object::x = x;}
    void SetY(float y) {Object::y = y;}

    int GetBoundX() {return boundX;}
    int GetBoundY() {return boundY;}

    int GetID() {return ID;}
    void SetID(int ID) {Object::ID = ID;}

    bool GetAlive() {return alive;}
    void SetAlive(bool alive) {Object::alive = alive;}

    bool GetCollidable() {return collidable;}
    void SetCollidable(bool collidable) {Object::collidable = collidable;}

    bool CheckCollisions(Object *otherObject);
    void virtual Collided(int objectID);
    bool Collidable();
};

对象.cpp

#include "Object.h"

Object::Object()
{
    x = 0;
    y = 0;

    velX = 0;
    velY = 0;

    dirX = 0;
    dirY = 0;

    boundX = 0;
    boundY = 0;

    maxFrame = 0;
    curFrame = 0;
    frameCount = 0;
    frameDelay = 0;
    frameWidth = 0;
    frameHeight = 0;
    animationColumns = 0;
    animationDirection = 0;

    image = NULL;

    alive = true;
    collidable = true;
}

void Object::Init(float x, float y, float velX, float velY, int dirX, int dirY, int boundX, int boundY)
{
    std::cout << "HERE?" << std::endl;
    Object::x = x;
    Object::y = y;

    Object::velX = velX;
    Object::velY = velY;

    Object::dirX = dirX;
    Object::dirY = dirY;

    Object::boundX = boundX;
    Object::boundY = boundY;
}

调用代码

Garo *garo;
// ...
garo->Init(garo_img);

这是我收到运行时错误的地方。我正在使用 Allegro 库,因此请随时询问您可能看到的任何奇怪类型。我还在学习 C++,所以请帮助我理解一些基本的术语。

4

1 回答 1

5

您需要实例化一个对象实例来进行操作。例如:

garo = new Garo();

通过错过这一点,您试图在未初始化的变量上调用方法。您可能应该考虑使用某种形式的智能指针来确保对象被销毁。

于 2012-04-04T18:16:53.780 回答