[英]C++ error C2248: cannot access private member declared in SUPER class
I have looked through similar questions from stackoverflow but haven't found an answer yet. 我从stackoverflow看过类似的问题,但是还没有找到答案。
Here's my subclass declaration: 这是我的子类声明:
class Enemy : public Entity
{
public:
Enemy();
~Enemy();
}; // This is the line that shows the error...
Here's my superclass declaration: 这是我的超类声明:
class Entity
{
//Member Methods:
public:
Entity();
~Entity();
bool Initialise(Sprite* sprite);
void Process(float deltaTime);
void Draw(BackBuffer& backBuffer);
void SetDead(bool dead);
bool IsDead() const;
bool IsCollidingWith(Entity& e);
float GetPositionX();
float GetPositionY();
float GetHorizontalVelocity();
void SetHorizontalVelocity(float x);
float GetVerticalVelocity();
void SetVerticalVelocity(float y);
protected:
private:
Entity(const Entity& entity);
Entity& operator=(const Entity& entity);
//Member Data:
public:
protected:
Sprite* m_pSprite;
float m_x;
float m_y;
float m_velocityX;
float m_velocityY;
bool m_dead;
private:
};
I already have a subclass called playership using the same structure, but that one works fine. 我已经有一个使用相同结构的子类,称为playership,但该类很好用。 So where could went wrong?
那么哪里出了问题?
private:
Entity(const Entity& entity);
Entity& operator=(const Entity& entity);
You made the Entity class uncopyable and unassignalbe. 您已将Entity类设为不可复制且不可分配。 But your enemy class doesn't have these member functions declared.
但是,您的敌人的类没有声明这些成员函数。 So the compiler obliges and generates them for you.
因此,编译器必须为您生成它们。 No problem so far, but I assume you then attempt to copy an enemy...
到目前为止没有问题,但是我想您然后会尝试复制敌人...
A minimal example for getting a similar error: 出现类似错误的最小示例:
class Entity
{
//Member Methods:
public:
Entity();
~Entity();
private:
Entity(const Entity& entity);
Entity& operator=(const Entity& entity);
};
class Enemy : public Entity
{
public:
Enemy(){}
~Enemy(){}
};
int main()
{
Enemy e1, e2 = e1;
return 0;
}
Generally you cannot access private functions or constructor of superclass entity
,when you use object of a class first constructor of superclass is called and then constructor of subclass is called. 通常,当您使用类的对象时,您不能访问私有函数或超类
entity
构造函数,先调用超类的构造函数,然后再调用子类的构造函数。
Here constructor of your superclass is declared as private.So, when you use sub class enemy
first constructor of superclass entity
is called and then constructor of sub class enemy
is called . 在这里,您的超类的构造函数被声明为私有。因此,当您使用子类
enemy
先调用超类entity
构造函数,然后再调用子类enemy
构造函数。
In your code constructor of superclass entity
is private which is called first when you use object of subclass enemy
which cannot be accessed thats why error occur. 在您的超类
entity
代码构造函数中,私有是私有的,在您使用无法访问的子类enemy
对象时,该对象首先被调用,这就是为什么会发生错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.