need help with my sdl rpg game

i am writing an sdl rpg at the moment, and i have run into a bit of a problem.
i have one class for the player, wich contains all the atributes and stuff like that for the player. the problem is this:
in order to access these atributes i need to create an object right? then since i need them in multiple classes, i will need th create an object for the player in each of the classes its gonna be used (or?), that makes it so that i have multiple objects of the player class spread around in my engine, and changing the hp for example in one would not change the hp in the others (or?). any way to solve this, like making a universal object of some kind or something? or is it maybe possible to pass the objects around somehow?
i tried to create a predefined object of the player class like this:

player.h:
1
2
3
4
5
6
7
8
9
10
11
class Player {
	public:
		Player();
		int get_Health();
		int get_MaxHealth();
		void set_Health(int number);
		void update();

	private:
		int health, maxHealth;
}player();


gui.cpp;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include "GUI.h"
#include "sdl.h"
#include "player.h"

GUI::GUI (SDL_Surface* surface, Uint32 color){
	screen = surface;
	white = color;
}

void GUI::healthBar(){
	emptyBar.x = 20, emptyBar.y = 440;
	emptyBar.h = 20, emptyBar.w = Player.get_Health();

	hBar.x = 20, hBar.y = 440;
	hBar.h = 20, hBar.w = player.get_Health();
	SDL_FillRect (screen, &emptyBar, NULL);
	SDL_FillRect (screen, &hBar, white);
}


the error i get:
1
2
1>c:\users\even\desktop\lf - game engine\lf - game engine\gui.cpp(12): error C2228: left of '.get_Health' must have class/struct/union
1>c:\users\even\desktop\lf - game engine\lf - game engine\gui.cpp(15): error C2228: left of '.get_Health' must have class/struct/union


can someone tell me what i did wrong?
}player();

That is not how you create an object at the end of a class/struct, remove the ().

Player.get_Health();

player.get_Health();

Capitalization counts, make sure it stays consistent.
Last edited on
i removed the brackets and de-capitalized the words (i have no wonder why th p was capitalized)

now i get a different error message.

1
2
3
4
5
6
1
1>GUI.obj : error LNK2005: "class Player player" (?player@@3VPlayer@@A) already defined in cms.obj
1>main.obj : error LNK2005: "class Player player" (?player@@3VPlayer@@A) already defined in cms.obj
1>map.obj : error LNK2005: "class Player player" (?player@@3VPlayer@@A) already defined in cms.obj
1>player.obj : error LNK2005: "class Player player" (?player@@3VPlayer@@A) already defined in cms.obj
1>C:\Users\Even\Desktop\LF - game engine\Debug\LF - game engine.exe : fatal error LNK1169: one or more multiply defined symbols found
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========


what does this mean?
That is because you have included that header files in several .cpp files so it thinks the object is being defined several times as well (which is why you aren't meant to define anything like that in header files).

It would be a better idea to create the object in main or somewhere like that and pass it into the other class functions when needed. Instead of defining it globally.
or add this around your whole header:

player.h:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#ifndef PLAYER
#define PLAYER

class Player {
	public:
		Player();
		int get_Health();
		int get_MaxHealth();
		void set_Health(int number);
		void update();

	private:
		int health, maxHealth;
}player;

#endif 


edit: and you could use the object by passing a pointer to the same player object, that way you need only one instance.
Last edited on
script coder, if you mean the ifndef PLAYER etc, i already had that in, just didnt think of adding them here.

i have tried to only create the object in main, then passing it like a variable to the other functions. it seems to work fine, i just hav to rewrite abit of code for the code to do what i initially wanted it to be able to do.

thanks for the help, its really apreciated :)
@Script Coder
Include guards doesn't prevent player from being defined multiple times when the header is included in more than one source file.

I think James idea is good. Create the object you need in main or some other place and pass it by reference to the functions that needs it.
Last edited on
Topic archived. No new replies allowed.