When I try to access a bool variable from a object in another class I get nullptr when I start my application. I dont relly know why, I have try to make the object to a pointer and a normal but I dont get it right.
This is the code from where I create the variable
1 2 3 4 5 6 7 8 9 10
//main_guy.h
class main_guy : public Entity
{
public:
main_guy(sf::RenderWindow* window, Entitymanger* entitymanger, class Map* map, float x, float y);
bool update(sf::RenderWindow *window);
void collision(Entity* entity);
//this is the variable
bool Finish = false;
In this class I create the object
1 2 3 4 5 6 7 8 9 10 11 12
#include "main_guy.h"
class expbar : public sf::RectangleShape
{
public:
expbar(int x, int y, sf::RenderWindow* window, main_guy* Player);
void update(sf::RenderWindow* window);
private:
//here is the object I create when I try to access the bool varibale.
main_guy* Player;
//main_game header
#include "expbar.h"
#include "main_guy.h"
class main_game : public tiny_state
{
public:
//Här använder vi de virtuela funktionerna från game_state
void Initialize(sf::RenderWindow *window);
void Update(sf::RenderWindow *window);
void Render(sf::RenderWindow *window);
void Destroy(sf::RenderWindow *window);
private:
expbar* Expbar;
//I have just create a object as this, nothing more.
main_guy* player;
I have just create a object of the player class as a pointer
You have not created a Player object. You have created a pointer to a Player object.
The pointer has not been initialized. You might want to create the object with new as you do with expbar and Entitymanger.
Another option is to not use pointers.
1 2
// player is a main_guy object
main_guy player;
Pointers often complicate the code so it is generally best to only use them when you have a good reason for it. In this situation I don't really see the point of using a pointer (at least not in main_game class) but I only see part of your program so I can't say for sure.
When I have done that I get a new error in the expbar cpp file and reads like this
"no default constructor exists for class "main_guy" ". I slove this error to do like this
1 2 3 4 5 6
// I added this little text underneath
expbar::expbar(int x, int y, sf::RenderWindow* window) : player(player)
{//... Here is where I got the error before I slove it.
}
Then when I started the game it worked fine. But the object dosent read their own values right. Because when I started debugg and hold my mouse above the player it was written that every Player variable was 0 or false. I dont think the object is working correct?
1 2 3 4 5 6 7 8 9 10
void expbar::update(sf::RenderWindow* window)
{
// I hold my mouse above player
if (Player.Finish == true)
{
std::cout << "Add exp" << std::endl;
}
}
Oh, sorry, I got a bit confused. In the original code expbar got a variable named Player, and main_game got a variable named player, so when I saw player later I just assumed it was in the main_game class.
My suggestion about not using pointers was primarily aimed towards the main_game class.
If the expbar class needs access to the player object you would have to pass a pointer or reference, either to the constructor and store it in a variable like you did in your original code, or if you only need it inside the update function you could pass it as argument to that function instead.
If all the expbar needs is the experience value of the player you might want to consider decoupling the expbar class from the main_guy class by simply passing the experience value to the expbar somehow, instead of the whole player object.