2012-04-04 4 views
0

ausgewertet werden ich nicht die Elementwerte eines Klassenobjekts ändern kann wegen der oben genannten Fehler:CX0030: Fehler: Der Ausdruck kann nicht

Wenn ich meine Initialisierungsfunktion rufe meine „garo“ Objekt zu initialisieren, erhalte ich die folgender Laufzeitfehler:

"Nicht behandelte Ausnahme bei 0x01323976 in Heretic.exe: 0xC0000005: Zugriff auf Leseort 0x00000008 der Zugriffsverletzung."

HINWEIS: Die Klasse Garo ist ein Kind von Objekt.

DER CODE

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

... der Rest wurde abgekürzt

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

The Calling-Code

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

Dies ist, wo ich den Laufzeitfehler erhalten. Ich benutze Allegro-Bibliotheken, also fragen Sie nach irgendwelchen seltsamen Typen, die Sie sehen können. Ich lerne immer noch C++, also bitte helfen Sie mir in rudimentären Begriffen zu verstehen.

Antwort

5

Sie müssen eine Objektinstanz zur Bearbeitung instanziieren. Zum Beispiel:

garo = new Garo(); 

Wenn Sie dies verpasst haben, versuchen Sie, Methoden für eine nicht initialisierte Variable aufzurufen. Sie sollten in Betracht ziehen, eine Art Smart-Pointer zu verwenden, um sicherzustellen, dass das Objekt zerstört wird.

+0

... und geben Sie es frei, sobald Sie damit fertig sind. +1. – Cameron

+1

ES ARBEITET !!! Vielen Dank David! – user1313361

Verwandte Themen