0

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?

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
darclander
  • 1,526
  • 1
  • 13
  • 35
  • You have the same struct defined in three different places. It should be in only one place in a header file. – interjay Apr 18 '19 at 14:16
  • @interjay, yes removing the struct from player.cpp is working but if I am using the struct in main.cpp I need to declare it there aswell? – darclander Apr 18 '19 at 14:18
  • 1
    If you haven't gotten any good books to read yet, [here's a list](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282). Please get a couple of them and read *from the bginning*. It seems to me like you're just guessing about how C++ work, and that's not a good way to learn *any* language (programming or other). – Some programmer dude Apr 18 '19 at 14:21
  • 2
    `struct _Player` That identifier is reserved. By defining it, your program will have undefined behaviour. You should use another name for the class. – eerorika Apr 18 '19 at 14:30

2 Answers2

1

No you need to define class only once in header file(say player.h) and use #include player.h in all files that are class. If you have any function for player class it is a convention you declare them in header file and define them in .cpp file. You could also declare and define them in header file and avoid .cpp file but it can cause messy code.

Nevus
  • 1,307
  • 1
  • 9
  • 21
0

I solved the issue by renaming my class to an unused name, put the struct only in player.h, declared player1 in main.cpp and then sent player1 as I did to send it to the playerinfo2 function.

//in main.cpp
#include "player.h"

class MyClass test;


userPlayer player1;

void playerinfo2(userPlayer *self) {
    self->name = "test1";
}

int main() {
    playerinfo2(&player1);
    std::cout << player1.name;
    test.playerinfo(&player1);
    std::cout << player1.name;

}


//in player.h
#include <string>
#include <iostream>

struct userPlayer {
    std::string name;
    int hp;
    int mana;
    int def;
    int mana_regen;
    int hp_regen;
};

class MyClass {

public:

void player_update();
void playerinfo(userPlayer *self);

private:

};

//in player.cpp
#include "player.h"


void MyClass::playerinfo(userPlayer *self) {
    self->name = "test3";
}

This updated code is working for me.

darclander
  • 1,526
  • 1
  • 13
  • 35