Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Sharing SFML variable between 2 classes  (Read 2164 times)

0 Members and 1 Guest are viewing this topic.

danik550

  • Newbie
  • *
  • Posts: 3
    • View Profile
Sharing SFML variable between 2 classes
« 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
« Last Edit: May 21, 2019, 12:53:35 pm by danik550 »

Elias Daler

  • Hero Member
  • *****
  • Posts: 599
    • View Profile
    • Blog
    • Email
Re: Sharing SFML variable between 2 classes
« Reply #1 on: May 21, 2019, 12:58:36 pm »
Please post the error you're getting.
Did you include "player.h" in collider.cpp?
Tomb Painter, Re:creation dev (abandoned, doing other things) | edw.is | @EliasDaler

danik550

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Sharing SFML variable between 2 classes
« Reply #2 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?

Elias Daler

  • Hero Member
  • *****
  • Posts: 599
    • View Profile
    • Blog
    • Email
Re: Sharing SFML variable between 2 classes
« Reply #3 on: May 21, 2019, 02:29:15 pm »
You didn't define constructor for Player class, that's why linking fails.
Tomb Painter, Re:creation dev (abandoned, doing other things) | edw.is | @EliasDaler

danik550

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Sharing SFML variable between 2 classes
« Reply #4 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();

Stauricus

  • Sr. Member
  • ****
  • Posts: 369
    • View Profile
    • A Mafia Graphic Novel
    • Email
Re: Sharing SFML variable between 2 classes
« Reply #5 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.
Visit my game site (and hopefully help funding it? )
Website | IndieDB

 

anything