C++ "Unable to read memory" when accessing pointer object from inherited class

1

I have this error:

Exception thrown at 0x0108C6E9 in myprojectname.exe: 0xC0000005: Access violation reading location 0x00000028.

However, this only happens when I call a function from the base class via the derived class.

derived class:

#pragma once
#include "Player.h"

class Space;
class Enemy : public Player{
public:
    void init(Space * s);

private:
    //Space * space = nullptr;
};

CPP file:

#include "Space.h"
#include "Enemy.h"
#include "Player.h"

void Enemy::init(Space * s) {
    //space = s;
}

I need a pointer to the space object because my base class also needs that. I am not sure if this is needed.

This is how I call the functions of the base(Player) class:

space->getEnemy().drawPlayer(); //FIRST I GET THE OBJECT OF THE DERIVED(ENEMY CLASS) AND THAN I CALL A FUNCTION FROM THE BASE CLASS(PLAYER CLASSS)

The error message "Unable to read memory" happens in getter functions in a class that the base class needs(for example a pointer to the window).

Does anyone know what I am doing wrong?

EDIT

I forgot to tell: I initialize that space pointer to my import class named Space, in every class of my project. The program works fine with those space pointers if I don't call the inherited stuff of Enemy class

Space.h

#pragma once
#include "Window.h"
#include "Game.h"
#include "Input.h"
#include "Text.h"
#include "Player.h"
#include "Enemy.h"

#include <ctime>

class Space {
public:
    void init();
    int getStartTime();

    Window & getWindow();
    Game & getGame();
    Input & getInput();
    Text & getText();
    Player & getPlayer();
    Enemy & getEnemy();

private:
    Text textObj;
    Window windowObj;
    Game gameObj;
    Input inputObj;
    Player playerObj;
    Enemy enemyObj;

    int startTime = clock();
};

Space.cpp:

#include "Space.h"

//PASSING THE SPACE CLASS OBJECT TO THE INIT FUNCTIONS OF THE OTHER CLASSES
void Space::init(){
    windowObj.init(this);
    gameObj.init(this);
    inputObj.init(this);
    textObj.init(this);
    playerObj.init(this);
    enemyObj.init(this);

    //STARTING THE GAME ACTUALLY
    windowObj.createWindow();
}

Enemy & Space::getEnemy() {
    return enemyObj;
}

Window & Space::getWindow(){
    return windowObj;
}

Game & Space::getGame() {
    return gameObj;
}

Input & Space::getInput() {
    return inputObj;
}

Text & Space::getText() {
    return textObj;
}

Player & Space::getPlayer(){
    return playerObj;
}

int Space::getStartTime(){
    return startTime;
}

Player.h(has space pointer):

#pragma once

class Space;
class Player {
public:
    void init(Space * s);
    void drawPlayer();
    void setPlayerXSpeed(float speed);
    void move();
    void jump();
    void checkFalling();
    void setJumping(bool value);
    void setAttacking(bool value);
    void projectileAttack();

    float getPlayerX();
    float getPlayerY();

private:
    Space * space;

    int sprites = 8;
    int chanceToMove = 0;
    int times = 0;
    int spriteCount = 0;

    float spritePart = 0.0f;
    float spritePiece;
    float playerX = -0.7f;
    float playerY = -0.42f;
    float playerXSpeed = 0.0f;
    float playerYSpeed = 0.0f;

    bool ableToChangeY = true;
    bool jumping = false;
    bool falling = false;
    bool strafeRight = true;
    bool attacking = false;
};

Player.cpp:

#include "Player.h"
#include "Space.h"

#include <SDL2\SDL_opengl.h>
#include <iostream>

#define MAX_JUMP_HEIGHT 0.2f
#define SPAWN_X -0.42f

void Player::init(Space * s){ //INITIALIZING SPACE CLASS OBJECT
    space = s;
    spritePiece = 0.125f;
}

void Player::drawPlayer(){
    glPushMatrix();
        glEnable(GL_TEXTURE_2D);
            glEnable(GL_BLEND);
            glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
            glBindTexture(GL_TEXTURE_2D, space->getWindow().getTexture(1));
            glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
                glBegin(GL_QUADS);
                    //PART OF THE SPRITESHEET                     DRAWING COORDINATES
                    glTexCoord2f(spritePart, 0.0f);               if (strafeRight) { glVertex2f(playerX        , playerY + 0.52f); } else { glVertex2f(playerX        , playerY + 0.52f); }
                    glTexCoord2f(spritePart + spritePiece, 0.0f); if (strafeRight) { glVertex2f(playerX + 0.13f, playerY + 0.52f); } else { glVertex2f(playerX - 0.13f, playerY + 0.52f); }
                    glTexCoord2f(spritePart + spritePiece, 1.0f); if (strafeRight) { glVertex2f(playerX + 0.13f, playerY)        ; } else { glVertex2f(playerX - 0.13f, playerY        ); }
                    glTexCoord2f(spritePart, 1.0f);               if (strafeRight) { glVertex2f(playerX        , playerY)        ; } else { glVertex2f(playerX        , playerY        ); }
                glEnd();
            glDisable(GL_BLEND);
        glDisable(GL_TEXTURE_2D);
    glPopMatrix();
}

void Player::move(){
    chanceToMove++;
    if (!attacking){ //NORMAL WALK ANIMATION
        if (playerXSpeed != 0.0f) {
            if (spriteCount != sprites - 3) { //IF CURRENT PART IS NOT THE LAST PART
                if (chanceToMove % 4 == 0) { //SO THE ANIMATION IS NOT AS FAST AS SANIC IS
                    spritePart += spritePiece;
                    spriteCount++;
                }
            }else {
                spritePart = 0.0f;
                spriteCount = 0;
            }
        }else {
            spritePart = 0.0f; //ALSO RESET
        }
    }else { //ATTACKING
        spritePart = spritePiece * (sprites - 2); //ATTACK SPRITE
        projectileAttack();
        if (times == 5) {
            attacking = false;
            times = 0;
        }else {
            times++;
        }
    }

    if (playerXSpeed < 0) { //IF PLAYER IS MOVING LEFT
        strafeRight = false;
    }else {
        strafeRight = true; //MOVING RIGHT
    }

    if (jumping) {
        jump();
    }

    playerX += playerXSpeed; //UPDATING PLAYER POSITIONS
    playerY += playerYSpeed;
}

void Player::checkFalling() {
    if ((playerX < space->getGame().getPlatformLeft() - 0.1f || playerX > space->getGame().getPlatformRight() - 0.05f) && playerY < -0.4f) { //IF PLAYER JUMPED OFF THE PLATFORM
        ableToChangeY = false;
        playerYSpeed = -0.03f;
    }if (playerY < -2.0f) { //IF PLAYER FELL TO DEATH(RIP)
        space->getGame().stopGame();
    }
}

void Player::setJumping(bool value){
    jumping = value;
}

void Player::setAttacking(bool value){
    attacking = value;
}

void Player::projectileAttack(){

}

void Player::jump(){
    spritePart = spritePiece * (sprites-1); //JUMPING SPRITE
    if (ableToChangeY) { //IF PLAYER IS NOT FALLING RIP
        if (playerY < MAX_JUMP_HEIGHT && !falling) { //WHILE THE PLAYER DID NOT PASS THE MAXIMUM JUMP HEIGHT
            playerYSpeed = 0.03f;
        }else { //IF THE PLAYER JUMPED TOO HIGH
            falling = true;
            if (playerY > SPAWN_X) {
                playerYSpeed = -0.03f;
            }else { //PLAYER IS BACK ON EARTH
                jumping = false;
                falling = false;
                playerYSpeed = 0.0f;
            }
        }
    }
}

void Player::setPlayerXSpeed(float speed){
    playerXSpeed = speed;
}

float Player::getPlayerX()
{
    return playerX;
}

float Player::getPlayerY(){
    return playerY;
}

EDIT MAIN FUNCTION

#include "Space.h"
#include <iostream>
#include <SDL2\SDL.h>
#include <SDL2\SDL_opengl.h>
#include <SDL2\SDL_ttf.h>
#undef main

int main(int argc, char** argv) {
    SDL_Init(SDL_INIT_EVERYTHING);
    SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
    TTF_Init();

    Space space;
    space.init();

    std::cout << "Type something and press ENTER to quit..." << std::endl;
    char temp;
    std::cin >> temp;

    TTF_Quit();
    SDL_Quit();
    return 0;
}
c++
pointers
inheritance
asked on Stack Overflow Dec 8, 2015 by CPP_Newb69 • edited Dec 8, 2015 by CPP_Newb69

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0