Using multiple files

Feb 26, 2016 at 6:43am
How do I make my main.cpp realize that I have declared all my stat structs in another file? I have added that file in my project, but it still gives those "not initialized" errors. Help?
Feb 26, 2016 at 7:02am
Pls use " " and write your code in it,we will tell you where its wrong/wht to do based upon your
needs.
Feb 26, 2016 at 12:11pm
K.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
//structs.cpp

#include <iostream>

using namespace std;

struct character
{
int HP;
int ammo;
};

character player {500, 20};

//main.cpp

#include <iostream>

int main ()
{
std::cout << player.HP;
return 0;
}


I added structs.cpp to my project. But when I run it, it gives an error that player.HP is not initialized.
Feb 26, 2016 at 1:49pm
Why are you trying to use the global variable?

The problem is that main.cpp doesn't know how your structure is defined or how and what a player is defined.

I suggest you put your structure definition in a header file, and make your "player" a variable local to some function and pass that variable to and from your functions as required.

Topic archived. No new replies allowed.