Wilheim
Wilheim

Reputation: 123

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.

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. So where could went wrong?

Upvotes: 1

Views: 1506

Answers (2)

Generally you cannot access private functions or constructor of superclassentity ,when you use object of a class first constructor of superclass is called and then constructor of subclass is called.

Here constructor of your superclass is declared as private.So, when you use sub classenemy first constructor of superclassentity is called and then constructor of sub classenemy is called .

In your code constructor of superclassentity is private which is called first when you use object of subclassenemy which cannot be accessed thats why error occur.

Upvotes: 2

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

You made the Entity class uncopyable and unassignalbe. 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;
}

Upvotes: 2

Related Questions