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();
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
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 ==========
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.
#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.
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.