one or more multiply defined symbols found

Hey I'm creating a text based game and I'm getting really confused when I tried to separate the functions in different cpp files so I can clear my main.cpp file.
the errors I'm having right now are:
Error LNK1169 one or more multiply defined symbols found
Error LNK2005 "int playerchoice" (?playerchoice@@3HA) already defined in main.obj
Error LNK2005 "class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > name" (?name@@3V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@A) already defined in main.obj

Here is the code

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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
main.cpp

#include <Mission1.h>
#include "functions.h"

using namespace std;

int player()				// Setting The Player Info
{
	cout << "Enter Name: ";
	cin >> name;
	cout << "Choose a Dragon Slayer Magic: " << endl;
	cout << "1: Fire Dragon Slayer Magic" << endl;
	cout << "2: Shadow Dragon Slayer Magic" << endl;
	cout << "3: Iron Dragon Slayer Magic" << endl;
	cout << "4: Lightning Dragon Slayer Magic" << endl;
	cout << "5: Sky Dragon Slayer Magic" << endl;
	cout << "Selection: ";
	cin >> playerchoice;
	return playerchoice;
}

void FireDragonSlayerMagic()		// FIRE DRAGON SLAYER MAGIC
{
	cout << "1: Fire Dragon's Roar" << endl;
	cout << "2: Fire Dragon's Claw" << endl;
	cout << "3: Fire Dragon's Iron Fist" << endl;
	cout << "4: Fire Dragon's Wing Attack" << endl;
}

void ShadowDragonSlayerMagic()			//SHADOW DRAGON SLAYER MAGIC
{	
	cout << "1: Shadow Dragon's Roar" << endl;
	cout << "2: Shadow Dragon's Slash" << endl;
	cout << "3: Shadow Dragon's Crushing Fang" << endl;
	cout << "4: Shadow Dragon's Waxwing Flash" << endl;
}

void IronDragonSlayerMagic()			//IRON DRAGON SLAYER MAGIC
{
	cout << "1: Iron Dragon's Roar" << endl;
	cout << "2: Iron Dragon's Club" << endl;
	cout << "3: Iron Dragon's Sword" << endl;
	cout << "4: Iron Dragon's Scales" << endl;
}

void LightningDragonSlayerMagic()		//LIGHTNING DRAGON SLAYER MAGIC
{
	cout << "1: Lightning Dragon's Roar" << endl;
	cout << "2: Raging Bolt" << endl;
	cout << "3: Lightning Dragon's Iron Fist" << endl;
	cout << "4: Lightning Dragon's Heavenward Halberd" << endl;
}

void SkyDragonSlayerMagic()		//SKY DRAGON SLAYER MAGIC
{
	cout << "1: Sky Dragon's Roar" << endl;
	cout << "2: Sky Dragon's Healing Spell" << endl;
	cout << "3: Sky Dragon's Wing Attack" << endl;
	cout << "4: Vernier" << endl;
}


int main()					// Main Function
{
	player();
	system("cls");
	cout << "Welcome to Fairy Tail " << name << endl;
	cout << endl;
	Start();
	system("pause");
	return 0;
}


void Start()				//Start of the game
{
	cout << "Master Makarov is calling you in the Fairy Tail house" << endl;
	cout << endl;
	cout << "1 - Go to Fairy Tail house" << endl;
	cout << "2 - Explore Fiore" << endl;
	cout << "Selection: ";
	cin >> choice;
	switch (choice)
	{
	case 1:
		GuildHouse();
		break;
	case 2:
		ExploreFiore();
		break;
	default:
		break;
	}
}

void GuildHouse()			// Fairy Tail's Guild House Stages
{
	system("cls");
	Mission1Start();
	cout << "1 - Missions Table\n";
	cout << "Selection: ";
	cin >> choice;
	switch (choice)
	{
	case 1: MissionsBoard();
		break;
	default:
		break;
	}
}

void GuildHouseStage2()
{
	system("cls");
	cout << "1 - Missions Table\n";
	cout << "Selection: ";
	cin >> choice;
	switch (choice)
	{
	case 1: MissionsBoard();
		break;
	default:
		break;
	}
}

						//Exploring Fiore
void ExploreFiore()
{
	system("cls");
	int rndsel = 1+rand() % 4;
	switch (rndsel)
	{
	case 1:
		AngryErza();
		break;
	case 2:
		MeetNatsu();
		break;
	case 3:
		MeetGray();
		break;
	case 4:
		MeetLucy();
	default:
		break;
	}
	cout << "1: Keep Exploring" << endl;
	cout << "2: Go Back" << endl;
	cin >> choice;
	switch (choice)
	{
	case 1:
		ExploreFiore();
		break;
	case 2:
		system("cls");
		Start();
		break;
	default:
		break;
	}
}
							// Angry Erza
	void AngryErza()
	{
		cout << "A sword almost hits you." << endl;
		cout << "Then you see that Erza is chasing Natsu and Gray for eating her cake, again" << endl;
	}

							//Meet Natsu
void MeetNatsu()
{
	cout << "Natsu: Hey " << name << "! How are you doing?" << endl;
	cout << name << ": Nothing much, just walking around the city." << endl;
	cout << "What about you, Natsu?" << endl;
	cout << "Natsu: Just going into Lucy's house again." << endl;
	cout << name << ": Oh good! See you later then." << endl;
	cout << "Natsu: Yeah! See you later " << name << "." << endl;
}

							//Meet Gray
void MeetGray()
{
	cout << name << ": Hey Gray! You are naked, again?" << endl;
	cout << "Gray: Ah Crap!! Didn't realise it." << endl;
	cout << name << ": Yeah, like always." << endl;
	cout << name << ": Just watch out to not take off your underwear." << endl;
	cout << "Gray: Ah Yeah!! It happenes from time to time." << endl;
	cout << name << "," << "Gray: *laughs*" << endl;
}

							//Meet Lucy
void MeetLucy()
{
	cout << name << ": Hey, Lucy!" << endl;
	cout << "Lucy: Oh hey, " << name << "! What brings you here?" << endl;
	cout << name << ": Nothing. Just walking around." << endl;
	cout << name << ": What about you? I see you are holding a book." << endl;
	cout << "Lucy: Yeah I'm heading to the library to take it back and get a new one." << endl;
	cout << name << ": Alright. See you later Lucy." << endl;
	cout << "Lucy: See ya!" << endl;
}


void MissionsBoard()			// Missions Board
{
	system("cls");
	cout << "1 - Easy: Catch a thief" << endl;
	cout << "Selection: ";
	cin >> choice;
	switch (choice)
	{
	case 1:
		Mission1();
		break;
	default:
		break;
	}
}


void Mission1Complete()
{
	system("cls");
	cout << "Makarov: Great Job " << name << ". Atleast you didn't destroy the city :D!" << endl;
	GuildHouseStage2();
}

void Mission2()
{
	cout << "done" << endl;
}


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
Mission1.cpp

#include "Mission1.h"
#include "functions.h"

Mission1::Mission1()
{
	system("cls");
	cout << "The thief uses speed magic and you have to catch him before he escapes." << endl;
	cout << endl;
	switch (playerchoice)
	{
	case 1:
		cout << "You striked him down with your Fire Dragon's Roar" << endl;
		break;
	case 2:
		cout << "You striked him down with your Shadow Dragon's Roar" << endl;
		break;
	case 3:
		cout << "You striked him down with your Iron Dragon's Roar" << endl;
		break;
	case 4:
		cout << "You striked him down with your Lightning Dragon's Roar" << endl;
		break;
	case 5:
		cout << "You striked him down with your Wind Dragon's Roar" << endl;
	default:
		break;
	}
	cout << endl;
	cin.ignore();
	cout << "Press Enter to Continue";
	cin.get();
}

Mission1Start::Mission1Start()
{
	cout << "das" << endl;
}


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
49
functions.h

#ifndef FUNCTIONS_H
#define FUNCTIONS_H

#endif // !FUNCTIONS_H

#include <iostream>
#include <string>
#include <stdlib.h>
#include <ctime>

using namespace std;

//Functions

//Dragon Slayer Magics

void FireDragonSlayerMagic();
void ShadowDragonSlayerMagic();
void IronDragonSlayerMagic();
void LightningDragonSlayerMagic();
void SkyDragonSlayerMagic();

/*Starting Funcs*/
void Start();

/*Exploring Fiore Funcs*/
void ExploreFiore();
void AngryErza();
void MeetNatsu();
void MeetGray();
void MeetLucy();

/*GuildHouse Funcs*/
void GuildHouse();
void GuildHouseStage2();
void MissionsBoard();

/*Mission 1 Funcs*/

/*Mission 1 Funcs*/
void Mission2();

//Variables

string name;
int choice;
int playerchoice;


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Mission1.h

#pragma once

class Mission1
{
public:
	/*Mission 1 Funcs*/
	Mission1();
};

class Mission1Start
{
public:
	Mission1Start();
};
Last edited on
The file functions.h defines a global variable: int playerchoice;

The source file main.cpp includes the file functions.h, so when compiled (and turned into an object file) the source file main.cpp defines a global variable: int playerchoice;

The source fileMission1 includes the file functions.h, so when compiled (and turned into an object file) the source file Mission1 defines a global variable: int playerchoice;

When the linker joins together the two object files, it finds that both of them are trying to create a global variable with the same name. This is bad. Don't have two global variables with the same name.
The problem is that you have defined the variables in a header file so they are getting defined multiple times, once for each .cpp file that includes the header file.


If you are using a modern compiler you can solve this problem by making the variables inline.

1
2
3
4
// functions.h
inline string name;
inline int choice;
inline int playerchoice;


Otherwise you can declare them as extern in the header ...

1
2
3
4
// functions.h
extern string name;
extern int choice;
extern int playerchoice;

... and then define them as normal in a source file.

1
2
3
4
// functions.cpp
string name;
int choice;
int playerchoice;



Another alternative is of course to not use global variables in the first place.
Thanks alot to both of you!
I fixed my error and now the code looks much better.
Topic archived. No new replies allowed.