SFML community forums

Help => General => Topic started by: danik550 on May 21, 2019, 12:46:19 pm

Title: Sharing SFML variable between 2 classes
Post by: danik550 on May 21, 2019, 12:46:19 pm
Hey so basically I am developing a game and I came across a problem of not knowing how to use a variable in a class that has been made in a different class.

Basically

My player.h class looks like this

class Player
{
public:
       sf::Vector2f velocity;

and in the player.cpp I do things to this velocity.

However I want to use this variable for checking purposes in my collider.cpp class. How can I access this velocity variable in a different class?

I tried Player A;
A.velocity;

and so on but I ended up with some external library error.

I can screenshot code if it will help
Title: Re: Sharing SFML variable between 2 classes
Post by: Elias Daler on May 21, 2019, 12:58:36 pm
Please post the error you're getting.
Did you include "player.h" in collider.cpp?
Title: Re: Sharing SFML variable between 2 classes
Post by: danik550 on May 21, 2019, 01:30:31 pm
Please post the error you're getting.
Did you include "player.h" in collider.cpp?

Here is the error that pops up at runtime not as I put it in.
The error appears where ever I try putting it in.

Is this the right way of accessing other variables or am I doing something wrong?
Title: Re: Sharing SFML variable between 2 classes
Post by: Elias Daler on May 21, 2019, 02:29:15 pm
You didn't define constructor for Player class, that's why linking fails.
Title: Re: Sharing SFML variable between 2 classes
Post by: danik550 on May 21, 2019, 02:46:05 pm
You didn't define constructor for Player class, that's why linking fails.

Thanks!!!
I had a constructor but not the default one with no arguments

Is it okay that have both?

like this

Player(with arguments);
Player();
Title: Re: Sharing SFML variable between 2 classes
Post by: Stauricus on May 21, 2019, 03:51:08 pm
Yes, C++ will call the right one when you create the object

if you create a player object ike this
Player player(arguments)
it will call your fisrt constructor; and if you use
Player player;
it will call the second one.