我已经查看了来自 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/