C++ 错误 C2248 : cannot access private member declared in SUPER class

标签 c++ inheritance subclass superclass

我已经查看了来自 stackoverflow 的类似问题,但还没有找到答案。

这是我的子类声明:

class Enemy : public Entity
{
public:
    Enemy();
    ~Enemy();
}; // This is the line that shows the error...

这是我的父类(super class)声明:

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:

};

我已经有一个使用相同结构的名为 playership 的子类,但它工作正常。那么哪里出了问题呢?

最佳答案

private:
Entity(const Entity& entity);
Entity& operator=(const Entity& entity);

您使 Entity 类不可复制且不可分配。但是你的敌人类没有声明这些成员函数。所以编译器有义务为你生成它们。到目前为止没问题,但我假设您随后会尝试复制敌人...

出现类似错误的最小示例:

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

关于C++ 错误 C2248 : cannot access private member declared in SUPER class,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38632287/

相关文章:

c++ - move 赋值运算符和虚拟继承

c++ - 当 C++ 中的参数是父类类型时,如何调用子类的方法?

c++ - 在数组中查找重复项

c++ - 是否有理由在此代码中的字符串文字上使用 const_cast ?

python - 我没有正确使用setter方法吗?

python - 子类 str,并创建与 += 效果相同的新方法

iphone - 如何获取从标准 UIViewController 中调用的 UIWebView 的 UIScrollView 委托(delegate)方法?

c++ - 此 C++ 移动构造函数中是否存在内存泄漏?

c++ - 如何使用 Intel PIN 获取内存操作值?

python - 如何用抽象模型定义外键关系?