Declaring an object in a class

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>

using namespace std;
using namespace 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>

using namespace std;
using namespace sf;


class Game {

private:
	User someone;
public:
	static void build(RenderWindow &window);
	static void event(RenderWindow  &window, Event event);
	static void update(RenderWindow &window, Time deltaTime);
};
Last edited on
it gives me errors

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!
I am sorry I thought it would be too cluttered.

Heres the object being used in the cpp file:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
//#include "user.h"
#include "render.h"
#include <SFML/Graphics.hpp>
#include <iostream>

using namespace std;
using namespace sf;

// game construct
void Game::build(RenderWindow &window)
{
	window.clear(Color::White);
}

//event calls
void Game::event(RenderWindow  &window, Event event)
{
	if (event.type==event.MouseButtonPressed)
	{
		//get mouse position
		Mouse mouse;
		Vector2i mousePosition = mouse.getPosition();

		//start drawing
		
		someone.startDrawing(mousePosition);
	}
	if (event.type == event.MouseButtonReleased)
	{

	}
}

//update frame
void Game::update(RenderWindow &window, Time deltaTime)
{

}


Error is the following:
error:a nonstatic member reference must be relative to a specific object
I am sorry I thought it would be too cluttered.

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.

Why does event() need to be static?
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.
The keyword "static" means different things in different contexts. Where and how, exactly, are you defining the object as static?

And what do you mean by "ran out of namings"?
Last edited on
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.

See, for example:

http://www.tutorialspoint.com/cplusplus/cpp_static_members.htm

(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?
Last edited on
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.
You're welcome! Glad it worked out :)

(And sorry if I was overly sarcastic earlier.)
Topic archived. No new replies allowed.