[英]C++ “Unable to read memory” when accessing pointer object from inherited class
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. 但是,只有当我通过派生类从基类调用函数时,才会发生这种情况。
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: 这就是我调用base(Player)类的函数的方式:
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). 错误消息“无法读取内存”发生在基类需要的类的getter函数中(例如,指向窗口的指针)。
Does anyone know what I am doing wrong? 有人知道我在做什么错吗?
I initialize that space pointer to my import class named Space, in every class of my project. 我在项目的每个类中初始化指向我的导入类Space的空间指针。 The program works fine with those space pointers if I don't call the inherited stuff of Enemy class. 如果我不调用Enemy类的继承对象,则该程序可以与这些空间指针配合使用。
EDIT: 编辑:
I found out that the object to the (very important) Space class, is NULL in the base class. 我发现(非常重要的)Space类的对象在基类中为NULL。 The enemy class space object is not NULL, but the space object of the class it inherits from, is NULL. 敌人的类空间对象不是NULL,但是其继承的类的空间对象是NULL。 Does anyone know how an inherited pointer object can be NULL? 有谁知道继承的指针对象如何可以为NULL?
A picture to make it clearer: 图片更清晰:
http://prntscr.com/9btfim http://prntscr.com/9btfim
EDIT 2 MY CURRENT CODE: 编辑2我的当前代码:
Enemy.h (THE DERIVED CLASS) Enemy.h(派生类)
#pragma once
class Space;
class Enemy : public Player{
public:
void updateEnemy();
private:
};
Enemy.cpp 敌人
#include "Space.h"
#include "Enemy.h"
#include "Player.h"
#include <iostream>
void Enemy::updateEnemy(){
if (space == nullptr) {
std::cout << "null"; //IT PRINTS NULL
}
}
A part of player.h (THE BASE CLASS) player.h的一部分(基础类)
class Space; //forward declaration
public:
void init(Space * s);
protected:
Space * space;
A part of player.cpp player.cpp的一部分
void Player::init(Space * s){ //INITIALIZING SPACE CLASS OBJECT
space = s;
}
Both your Player object and your Enemy object declare a 您的Player对象和敌人对象都声明一个
Space * space;
So Enemy::space
hides Player::space
so when you init Enemy::space
, Player::space
remains zero. 因此, Enemy::space
隐藏了Player::space
因此当您初始化Enemy::space
, Player::space
保持为零。
It is almost certainly a mistake to declare another Space * space
in the Enemy
class. 在Enemy
类中声明另一个Space * space
几乎几乎是错误的。 You should be using the one in the Player
class. 您应该使用Player
类中的那个。 I expect the one in the Player
class was private
so you can't use it. 我希望Player
类中的那个是private
所以您不能使用它。 But having another one doesn't fix that. 但是拥有另一个并不能解决这个问题。 Either change it from private
to protected
or make all accesses through an accessor function. 可以将其从private
更改为protected
也可以通过访问器功能进行所有访问。
Edit: Wild guess, you want one Space * space
shared by all instances of the Player
class. 编辑:疯狂的猜测,您想要一个Space * space
由Player
类的所有实例共享。 To do that: Inside the definition of Player
you have: 为此:在Player
的定义中,您具有:
static Space * space;
and elsewhere outside the definition of Player
(in some cpp file) you need: 和Player
定义之外的其他地方(在某些cpp文件中),您需要:
Space* Player::space;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.