I am working on a game where I am currently trying to make a player struct to store player information such as name for example. I would like to make a class to divide the code segments for cleaner code in my main.cpp.
I have my struct:
//in main.cpp
#include "player.h"
class MyClass player
struct _Player {
std::string name;
int hp;
int mana;
int def;
int mana_regen;
int hp_regen;
} player1;
void playerinfo2(_Player *playerx) {
playerx->name = "test2";
}
int main() {
player1.name = "test1";
std::cout << player1.name << std::endl;
playerinfo2(&player1);
std::cout << player1.name;
player.playerinfo(&player1);
}
This code is working and is changing the name of player1 from "test1" to "test2".
//in player.h
class MyClass {
struct _Player {
std::string name;
int hp;
int mana;
int def;
int mana_regen;
int hp_regen;
};
public:
void player_update();
void playerinfo(_Player *self);
private:
};
I dont know if the struct needs to exist both in the header and in the .cpp file but it says "unknown _Player" if I do not.
//in player.cpp
#include "player.h"
struct _Player {
std::string name;
int hp;
int mana;
int def;
int mana_regen;
int hp_regen;
};
void Player::player_update() {
playery.mana = playery.mana + playery.mana_regen;
}
void Player::playerinfo(_Player *self) {
self->name = "test3";
}
When running in console g++ main.cpp player.cpp -o test.exe I receive the error message:
main.cpp: In function 'int main()':
main.cpp:29:29: error: no matching function for call to 'Player::playerinfo(_Player*)'
test.playerinfo(&player1);
^
In file included from main.cpp:4:0:
player.h:21:6: note: candidate: void Player::playerinfo(Player::_Player*)
void playerinfo(_Player *self);
^~~~~~~~~~
player.h:21:6: note: no known conversion for argument 1 from '_Player*' to 'Player::_Player*'
Is there any way I can pass my player1 to the class like I am doing with my function in main.cpp?