Hi there. I have 2 classes in 2 separate header files and I am trying to declare an object of one of the classes in the other class. However whenever I try to use the object in the cpp file it gives me errors.
Heres class 1:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#pragma once
#include <SFML/Graphics.hpp>
#include <iostream>
usingnamespace std;
usingnamespace sf;
class User
{
private:
Vector2i initialPosition;
Vector2i finalPosition;
Vector2i deltaPosition;
public:
void startDrawing(Vector2i mousePosition);
void drawLine();
//void
};
Heres class 2:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#pragma once
#include "user.h"
#include <SFML/Graphics.hpp>
#include <iostream>
usingnamespace std;
usingnamespace sf;
class Game {
private:
User someone;
public:
staticvoid build(RenderWindow &window);
staticvoid event(RenderWindow &window, Event event);
staticvoid update(RenderWindow &window, Time deltaTime);
};
Well done for realising that it's best not to tell us what the errors are, or show us the code that generates those errors. Remember, the less information you give us, the easier it is for us to help you!
Which is why the introductory information on these forums asks you to post a minimal but complete compiling example that reproduces your problem.
Game::event() is a static method. That means it doesn't run on a specific instance of the class; rather, it is general to all instances of the class simultaneously. This means that it can't access a non-static data member - because that data member must be specific to a single instance of the class.
ohh! None of them need to be static, I just didnt want to create an object of the class because I ran out of namings :P. However theres something interesting once I make the object itself static. I get the error: error LNK2001: unresolved external symbol
I am not sure I grasp the concept of static entirely.
I was defining it static in the Game class by just writing static User someone;.
And for the naming thing, I just needed one object of the class, no more. I thought it made sense that I eliminated the need for creating an object by using the static keyword.
I was defining it static in the Game class by just writing static User someone;.
That means that all instances of the Game class share the same instance of User. However, that's just a declaration; you'll need to define the object somewhere too.
(that was just the first useful link that came from Googling "C++ static data member".)
I don't understand why you're trying to confuse and complicate things. So you want a single User object - just create one! Why try and jump through hoops and confuse the design by doing something different?
Yeah yeah, it was a bad move from me. Anyway, thank you so much for not only solving my problem but also for making sure I understood where I went wrong. Thank you kind sir.