SFML community forums
Help => General => Topic started 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
-
Please post the error you're getting.
Did you include "player.h" in collider.cpp?
-
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?
-
You didn't define constructor for Player class, that's why linking fails.
-
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();
-
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.