Header file problem

So I am creating a header file to be used in my .cpp source files. The header file is done correctly (appears to be) and if I include it and use it in just one source file it works fine, but as soon as I include the header file in two source files (one having main function and the other containing the rest of the functions)I get an error.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#ifndef STRUCTURES_H_INCLUDED
#define STRUCTURES_H_INCLUDED



#define n2 "\n\n"
#define arrow "--> "
#define seperator "<-------------------------------------------------->"
#define center "\t\t\t"


using namespace std;

struct menu
{
    string username;
}name;

struct equipment_statistics
{
    string weapon = "Nothing but your bare hands";
    string legs_armor = "Just a pair of regular brown pants";
    string chest_armor = "Just a plain cotton shirt";

    int hp = 1000;
    int hunger = 0;
}equip;


#endif // STRUCTURES_H_INCLUDED 


If I include it in two soucre files(same project) i get two errors.

Multiple Definition of 'name'
multiple definition of 'equip'
Last edited on
The error you are getting is right. Only your main function should have the function definitions the other file code should just contain the functions.
So I can never use the same header file outside the main function (or in two different files)?
No you can have header files included in multiple files. I believe the issue is your header guards.
generally they are just #ifndef STRUCTURES_H your final underscore included shouldn't be there
So if I were to remove that _INCLUDED it should fix the problem and make them accessible?
I would think thats your problem but i also just tested it on g++ and g++ allows it either way. what compiler are you using?
what is the exact error you get?
Last edited on
When I go to codeblocks file > properties > advanced...
it says GNU GCC Compiler....is that right?
i figured it out i think. its because your creating an instance of the structs right after you declare them so it ends up trying to create multiple structs called name or equip.
Last edited on
Here is the two source files

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>
#include <string>
#include "structures.h"

using namespace std;

int equip_stats();
int menu();
int start();

int main()
{
    menu();
    start();


    cin.get();
    cin.get();
    return 0;
}

int equip_stats()
{
    cout << n2 << seperator << n2;
    cout << "Weapon - " << equip.weapon << endl;
    cout << "Leg Armor - " << equip.legs_armor << endl;
    cout << "Chest Armor - " << equip.chest_armor << n2;
    cout << "Health - " << equip.hp << " hp" << endl;
    cout << "Hunger - " << equip.hunger << " hunger" << n2;
}

int menu()
{

    cout << n2 << "<-----------------------------S1L3NTSH@D0WS----------------------------------->" << n2 << n2;
    cout << center << "Enter your username" << endl;
    cout << center << arrow;
    cin >> name.username;
    cout << n2 << center << "Welcome To My Game " << name.username << endl;
    
    cout << center << "Don't Mess up." << endl;
    cout << center << "Have Fun." << n2;
    cout << center << "Coded By S1L3NT" << n2;
    cout << "Press <Enter>";
    cin.get();
    cin.get();
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <string>
#include "structures.h"

using namespace std;

int equip_stats();
int menu();
int start();


int start()
{


}



and the errors...



obj\Debug\starting.o||In function `Z5startv':|
D0WS\structures.h|19|multiple definition of `name'|
D0WS\main.cpp|12|first defined here|
obj\Debug\starting.o||In function `Z5startv':|
D0WS\structures.h|19|multiple definition of `equip'|
D0WS\main.cpp|12|first defined here|
||=== Build finished: 4 errors, 0 warnings (0 minutes, 0 seconds) ===|
Last edited on
i edited my above post. its simply because you create an instance of the structs right after you declare them in the header file so you end up with multiple structs named name or equip.
Last edited on
Thank yo for your help so far, and being patient...I am still very new to c++ so what do you mean by "create an instance of the structs right after you declare them"? Maybe because its late...but im not quite understanding.
this is how your creating an instance right after your delcaring it
 
}name;

if you remove the name and then in main do
 
menu name;
Last edited on
Gotcha! I understand now! Thank you!!
Topic archived. No new replies allowed.