2012-04-04 130 views
0

我不能修改,因爲上述錯誤的類對象的成員值:CX0030:錯誤:表達式無法評估

當我打電話給我的初始化功能以初始化我的「GARO」對象時,收到以下運行時錯誤,

「Heretic.exe中的0x01323976未處理的異常:0xC0000005:訪問衝突讀取位置0x00000008」。

注意:類Garo是Object的子項。

守則

Garo.h

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

Garo.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; 
} 

...其餘已被縮略

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

Object.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++,所以請幫助我以最基本的方式理解。

回答

5

您需要實例化一個對象實例進行操作。例如:

garo = new Garo(); 

由於錯過了這一點,您正嘗試調用未初始化變量的方法。您應該考慮使用某種形式的智能指針來確保對象被銷燬。

+0

...一旦你完成使用它,並釋放它。 +1。 – Cameron 2012-04-04 18:18:25

+1

它工作了!非常感謝大衛! – user1313361 2012-04-04 18:24:46