简体   繁体   English

C ++错误C2248:无法访问在SUPER类中声明的私有成员

[英]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.

相关问题 错误C2248:无法访问在类中声明的私有成员 - Error C2248:cannot access private member declared in class 错误C2248:无法访问类中声明的私有成员 - error C2248: cannot access private member declared in class C2248:无法访问类中声明的私有成员 - C2248: Cannot access private member declared in class 错误C2248:无法访问在类中声明的受保护成员 - error C2248: cannot access protected member declared in class 错误C2248:无法访问类中声明的受保护成员 - error C2248 : cannot access protected member declared in class CPtrList 无法使用:错误 C2248:“CObject::operator =”:无法访问类“CObject”中声明的私有成员 - CPtrList cannot use: error C2248: 'CObject::operator =' : cannot access private member declared in class 'CObject' 无法访问在类'QReadWriteLock'中声明的私有成员错误1错误C2248:'QReadWriteLock :: QReadWriteLock' - Cannot access private member declared in class 'QReadWriteLock'Error 1 error C2248: 'QReadWriteLock::QReadWriteLock' 错误C2248:“ Gdiplus :: Bitmap :: Bitmap”:无法访问在类“ Gdiplus :: Bitmap”中声明的私有成员 - error C2248: 'Gdiplus::Bitmap::Bitmap' : cannot access private member declared in class 'Gdiplus::Bitmap' 错误C2248:“ klientPracownik :: klientPracownik”:无法访问在类“ klientPracownik”中声明的私有成员 - error C2248: 'klientPracownik::klientPracownik' : cannot access private member declared in class 'klientPracownik' 错误C2248:'CvSVM :: CvSVM':无法访问类'CvSVM'中声明的私有成员 - error C2248: 'CvSVM::CvSVM' : cannot access private member declared in class 'CvSVM'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM