Class inaccessible pointer - c++

Hello need your help to solve this problem or just explain me why my pointer is inaccessible .
PlayerSprite.h
#pragma once
#include "LoadPlayerRes.h"
#include "Keyboard.h"
#include "PlayerState.h"
class PlayerSprite :public LoadPlayerRes{
public:
PlayerSprite(float x, float y,float speed)
:
x(x),
y(y),
speed(speed)
{
pCurrentState = new StateStand(&Stand);
}
void Controls(Keyboard& kbd) {
speed = 0;
if (speed == 0) {
delete pCurrentState;
pCurrentState = new StateStand(&Stand);
}
}
void Draw(Graphics& gfx) {
pCurrentState->pSprite->Draw(x, y, gfx);// pSprite inaccessible
}
private:
float x, y,speed;
PlayerState* pCurrentState;
};
PlayerState.h
#pragma once
#include "SurfaceAnimation.h"
class PlayerState {
public:
PlayerState(SurfaceAnimation* pSprite)
:
pSprite(pSprite)
{}
protected:
SurfaceAnimation* pSprite;
};
class StateStand :public PlayerState {
public:
StateStand(SurfaceAnimation* pSprite)
:
PlayerState(pSprite)
{}
};
so i am trying to make a player state machine ,PlayerState class job is to point to right SurfaceAnimation object and based on pointer i will draw a player inside PlayerSprite class , but for some reason pSprite is inaccessible.

It's inaccessible because you made it protected.
So, only functions in PlayerState (or functions in classes inheriting PlayerState), can see it.
You're trying to access it from a function in PlayerSprite.

Related

Why can't I declare a data member from another class private within my class definition

I am getting a compiler error saying that the data member Point p is private within the context, when I declare Point p as private within class circle. The code and compiler error are below.
#include<iostream>
#include<vector>
class Point
{
public:
Point(double a, double b)
{
x = a;
y = b;
}
virtual ~Point(){}
private:
double x;
double y;
};
The code for the class shape and circle are as follows:
class shapes {
public:
virtual Point centre() const = 0;
virtual void draw() const = 0;
virtual void rotate(int angle) const = 0;
virtual ~shapes(){}
};
class circle: public shapes {
public:
Point centre() const override { return p; }
void draw() const override { }
void rotate(int angle) const override {}
virtual ~circle() {}
circle(Point x, int r):p{x},radius{r}{}
private:
Point p;
int radius; };
Edit: Smiley face class inherits from circle class with code below:
class smiley: public circle
{ //smiley face is a circle + eyes and mouth
public:
smiley(Point p, int r):circle{p,r},mouth{nullptr}{}
Point centre() const override { return p;}
void draw() const override
{
//draw circle
circle::draw();
for(auto e:eyes)
{
e->draw();
}
mouth->draw();
}
void rotate(int angle) const {}
virtual ~smiley()
{
delete mouth;
for (auto eye : eyes) //why not delete [] eyes
{
delete eye;
}
}
private:
std::vector<shapes*> eyes; //smiley face has eyes
shapes* mouth; //smiley face has a mouth
};
If I make the data member p public in the class circle, everything works. The compiler error is listed below:
Why can I not define the Point object p, in the circle class private?
Edit: I have added the compiler error message and added the missing code asked for in the comments below. Would you be able to re-open the question?
Private class members can only be accessed within the class or by friends, so, if you would like it to be accessed outside the class by a non-friend, you would need to use a setter/getter.

I'm having trouble figuring out how to access a variable outside of a nested class

The nested class can't access non-static elements that are outside of the class
Something is wrong with nested classes, as you can see by the code i've provided, 2 classes have been declared, the nested class Entity is inside the private field of the class Game, for some reason the nested class doesn't allow me to access the variable outside of it map_info.
I've only found 1 solution and it involves creating an instance of the Game class inside the Entity class.
class Game
{
private:
std::string map_info;
class entity
{
protected:
double x, y, z;
public:
};
class player : public entity
{
private: std::string name;
player(std::string _name) : name(_name) {};
void check()
{
/*
this is the portion of the code that i'm having problem with
the error: "a nonstatic member reference must be relative to a specific object"
*/
if (map_info != 'none')
{
// do stuff
}
}
};
public:
std::string Getmap() { return map_info; }
Game() //constructor
{
// does stuff
}
};
Here's something i tried doing
Declaring map_info as a static type (doesn't solve the problem)
Declaring an instance of the game class inside of the nested class
I've searched here for the same problem but the results don't fit my original code, however there might be a slight chance that i've missed something and if so please send the link.
It's my first question in this website so give as much feedback as you can, even the slightest is appreciated! Thanks!
That doesn't works as you want. Nested class is just class-member of your Game class, which can be accessed as Game::NestedName if it's public, and cannot be accessed not in class if it's private. For example
#include <vector>
class Game
{
public:
Game(){};
void spawnNewPrivateEntity(double x, double y, double z)
{
private_entities.push_back(PrivateEntity{x, y, z}); //access to private member PrivateEntity is ok
}
struct PublicEntity { double x, y, z; }; //public nested struct, can be accessed from everywhere
void spawnNewPublicEntity(PublicEntity entity)
{
public_entities.push_back(entity);
}
private:
struct PrivateEntity { double x, y, z; }; //private nested struct, can be used only inside this class or friend members
std::vector<PrivateEntity> private_entities = {};
std::vector<PublicEntity> public_entities = {};
}
int main()
{
auto en = Game::PublicEntity{0.0, 9.0, 1.9}; //works
auto en2 = Game::PrivateEntity{0.0, 0.0, 0.0}; //doesn't work: this struct is private member of Game class
}
You can make something like this:
class Game
{
public:
Game(std::string map_info): map_info(map_info)
{};
std::string getMapInfo() { return map_info; }
private:
std::string map_info;
};
class Entity
{
public:
Entity(Game *game): game(game)
{};
void check()
{
if (game->getMapInfo() != "none") { /*some code*/ }
};
private:
Game *game;
}

C++ Protected inheritance

ok so i've just encountered some weird problem.
i have the following code:
#pragma once
#include "Point.h"
#include "Shape.h"
class Polygon : public Shape{
public:
Polygon(int = 0);
Point& operator[] (int) const;
protected:
Point *Points;
private:
int Num_Of_Points;
};
Cpp file
Polygon::Polygon(int Num) : Num_Of_Points(Num) { Points = new Point[Num_Of_Points](); }
Point& Polygon::operator[] (int index) const{ return Points[index]; }
2nd header.
#pragma once
#include "Polygon.h"
class Triangle : public Polygon{
Triangle(Point, Point, Point);
private:
double Area;
};
Triangle::Triangle(Point A, Point B, Point C) : Polygon (3) {
Points[0] = A;
Points[1] = B;
Points[2] = C;
}
now i get the following error at Triangle constructor:
Error 1 error C2248: 'Polygon::Points' : cannot access private member declared in class 'Polygon'
i cant understand why, Points is protected member and not private (i get the same error when i put it under public aswell)...
help would be much appriciated!
EDIT: forgot to mention, polygon is abstract class, it inherit from shape some abstract functions.

How to access Constructor of Parent class in C++ with parent class having a pure virtual function

I am trying to create Circle and Rectangle from Class Shape. I want y to be assigned pi if I call Shape() constructor with a parameter (from circle class). Since Shape has a purely virtual function the compiler is showing error. How can I overcome this error. And why is default parameter running correctly then?
Also I tried this->Shape(0) from Circle class. Compiler is saying "Invalid use of this"
#include<iostream>
using namespace std;
class Shape
{public:
double x,y;
Shape()
{x=0;y=0;}
Shape(int p,int t=3.14159)
{x=p;y=t;}
virtual void display_area()=0;
virtual void get_data()=0;
};
class Circle: public Shape
{public:
Circle()
{Shape(0);} //ERROR HERE
void get_data()
{cout<<"\nRadius: ";cin>>x;}
void display_area()
{cout<<"\nArea: "<<y*x*x;}
};
base classes are always initialized before the constructor's block runs, so you do it in the constructor's member initialization list..
I also fixed another bug in your code.... You are doing some narrowing conversions, which wouldn't work as you want...
#include<iostream>
using namespace std;
class Shape
{
public:
double x,y;
Shape()
{
x=0;
y=0;
}
Shape(double p, double t=3.14159) //changed from Shape(int p, int t=3.14159)
{
x=p;
y=t;
}
virtual void display_area()=0;
virtual void get_data()=0;
};
class Circle: public Shape
{
public:
Circle() : Shape(0)
{ /*Shape(0); */ } //Not HERE
void get_data()
{
cout<<"\nRadius: ";
cin>>x;
}
void display_area()
{
cout<<"\nArea: "<<y*x*x;}
};
To call the base constructor you need to use the member initialization list.
Change:
Circle()
{
Shape(0);
} //ERROR HERE
To
Circle() : Shape(0)
{
}

Initiating a derived class with specific variable values

For the below code snippet, how do I initialize instances of class Enemy with variables (such as x, y, type)? I have it working correctly, it triggers the instances no matter how many of them I insert... I just need to know the best way of creating an enemy with certain variables that will differ for each of my instances... particularly when some of those variables are in the base class and others are not.
class BaseObject
{
public:
virtual void Render() = 0;
int x;
int y;
};
class Enemy : public BaseObject
{
public:
Enemy() { }
virtual void Render()
{
cout << "Render! Enemy" << endl;
}
typedef std::set<BaseObject *> GAMEOBJECTS;
GAMEOBJECTS g_gameObjects;
int main()
{
g_gameObjects.insert(new Enemy());
g_lootObjects.insert(new Loot());
for(GAMEOBJECTS::iterator it = g_gameObjects.begin();
it != g_gameObjects.end();
it++)
{
(*it)->Render();
}
for(GAMEOBJECTS::iterator it = g_lootObjects.begin();
it != g_lootObjects.end();
it++)
{
(*it)->Render();
}
return 0;
}
Include the arguments in the enemy constructor and Base constructors. You can then use those to initialize the member variables.
class BaseObject
{
public:
BaseObject(int x, int y) : x(x), y(y){ }
virtual void Render() = 0;
int x;
int y;
};
and
class Enemy : public BaseObject
{
public:
Enemy(int x, int y, int foo) : BaseObject(x,y), foo(foo) { }
int foo;
...
};