error LNK2005 & LNK1169 - Using external functions

***SOLVED***
The problem was that I added an additional .cpp to the Project, it was in the Source Files of MSVS (See red marking in the picture below). I did that because i was not familiar with the program and I thought it was the same as drag & dropping a text file to create an extra tab (so I can keep working at the file).

Later I renamed the file IN MSVS (red marking) to ".h" and ".hpp" both didnt work, BECAUSE the File was initially added as a CPP file. So MSVS used the file like a .cpp file the whole time, it didnt matter if I renamed it in the MSVS, i had to kick it out and readd it as an .hpp. Now it also has a little "h" symbol in MSVS.

So the Compiler cant Compile 2 files at once, header files arent compiled so its ok to have 1 cpp and X hpp's


http://i.imgur.com/dYuhlOX.png

***


Hello!
Im learning C++ and now I have encountered a problem that I cannot solve on my own. Im playing around with random numbers and external functions, I have encountered so many weird error messages and mistakes that I can barely keep my head up straight anymore an I need help.

The Goal of the Code is to Generate a random Name and it used to work when I had 1 Function that generated a First & Last Name. The problems started since I divided it up into 2 Functions (1 should generate the First Name, the other should Generate the Last name).

I initially did it because when I had only 1 function, I did not know how to return both the First and Last name Variables to the Main function (if anybody could also teach me how to do that, returning 2 strings in 1 function, that would be nice).

Since then I encountered a lot of problems and I dont know wich of my actions caused it but here are some suspects:

- Missing "#include" ?
- Missing commands in the function brackets ? "()"
- Something inside MSVS express 2013.

Im suspecting these because:
At one point I erased some #include functions I didnt need anymore (maybe I actually do need one but i cant remember all those that I removed).
At one point I relocated the .exe and the cpp file to another folder, but since then I recreated everything and MSVS tells me both .cpp's are in the same folder, so it shouldnt be it but im very new to MSVS.

Variables have german names, sry about that.
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
#include "stdafx.h"
#include <iostream>
#include <string>
#include "SpielerNameGenerator.cpp"

using namespace std;

string VorNameGenerieren ();
string NachNameGenerieren ();

int _tmain(int argc, _TCHAR* argv[])

{
	
		string VorName;
		string NachName;

	VorName = VorNameGenerieren ();
	NachName = NachNameGenerieren ();

	cout << endl << VorName << " " << NachName;

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



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
#include "stdafx.h"
#include <iostream>
#include <windows.h>
#include <time.h>
#pragma comment (lib, "winmm.lib")
#include <stdlib.h>
#include <string>



using namespace std;

string VorNameGenerieren(); 
string NachNameGenerieren();


//-----------------------------------------------------------------------------------------
// Generate First Name

string VorNameGenerieren() {

	srand(timeGetTime());
	int VorNameZahl = (rand() % 10 + 1); // Generates a Random Number between 1-10
	string VorName;
	switch (VorNameZahl)    {

	case 1:

		VorName = "Ryan";
		cout << "Dein Name ist " << VorName;
		break;

	case 2:

		VorName = "Norbert";
		cout << "Dein Name ist " << VorName;
		break;

	case 3:

		VorName = "Seth";
		cout << "Dein Name ist " << VorName;
		break;
	case 4:

		VorName = "Richard";
		cout << "Dein Name ist " << VorName;
		break;

	case 5:

		VorName = "Heinrich";
		cout << "Dein Name ist " << VorName;
		break;

	case 6:

		VorName = "Vio";
		cout << "Dein Name ist " << VorName;
		break;

	case 7:

		VorName = "Jason";
		cout << "Dein Name ist " << VorName;
		break;

	case 8:

		VorName = "Jonas";
		cout << "Dein Name ist " << VorName;
		break;

	case 9:

		VorName = "Viktor";
		cout << "Dein Name ist " << VorName;
		break;

	case 10:

		VorName = "Joffrey";
		cout << "Dein Name ist " << VorName;
		break;

		
	}
	return VorName;
}


// -----------------------------------------------------------------------------------
// Generate Last Name

string NachNameGenerieren () {

	srand(timeGetTime());
	int NachNameZahl = (rand() % 10 + 1); // Generates a Random Number between 1-10
	string NachName;
	switch (NachNameZahl)    {

	case 1:

		NachName = "McLaughlin";
		cout << " " << NachName << ".";
		break;

	case 2:

		NachName = "Sansalone";
		cout << " " << NachName << ".";
		break;

	case 3:

		NachName = "Kierstein";
		cout << " " << NachName << ".";
		break;

	case 4:

		NachName = "Kaszian";
		cout << " " << NachName << ".";
		break;

	case 5:

		NachName = "Jolly";
		cout << " " << NachName << ".";
		break;

	case 6:

		NachName = "McDonald";
		cout << " " << NachName << ".";
		break;

	case 7:

		NachName = "Smith";
		cout << " " << NachName << ".";
		break;

	case 8:

		NachName = "McFarlane";
		cout << " " << NachName << ".";
		break;

	case 9:

		NachName = "Gate";
		cout << " " << NachName << ".";
		break;

	case 10:

		NachName = "Stein";
		cout << " " << NachName << ".";
		break;
	}
	return NachName;
}



These are the error messages:

- Error 1 error LNK2005: "class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __cdecl NachNameGenerieren(void)" (?NachNameGenerieren@@YA?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@XZ) already defined in Player Spawn.obj C:\Users\Fab\Documents\Visual Studio 2013\Projects\Player Spawn\Player Spawn\SpielerNameGenerator.obj Player Spawn

- Error 2 error LNK2005: "class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __cdecl VorNameGenerieren(void)" (?VorNameGenerieren@@YA?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@XZ) already defined in Player Spawn.obj C:\Users\Fab\Documents\Visual Studio 2013\Projects\Player Spawn\Player Spawn\SpielerNameGenerator.obj Player Spawn

- Error 3 error LNK1169: one or more multiply defined symbols found C:\Users\Fab\Documents\Visual Studio 2013\Projects\Player Spawn\Debug\Player Spawn.exe 1 1 Player Spawn




I tried removing the Prototypes ...

"string VorNameGenerieren();
string NachNameGenerieren();"

... in either .cpp file and both. Same errors.



I relocated the .cpp files at one point but i reverted everything and even tried to create a new Project and rebuild it from scratch using the same names and same code, so im pretty sure thats not it. Thanks so much in advance.
Last edited on
Your name generator functions are being compiled more than once. That is what is causing your linker errors.

You are apparently including "SpielerNameGenerator.cpp" in more than once .cpp file, or Visual Studio is compiling that file in addition to your main.cpp. You should never #include a .cpp file.


Oh, but at one point it worked when I included it (I think at least >.<).

So can I save the same code as "SpielerNameGenerator.h" and include it?

edit: tried to rename it to "SpielerNameGenerator.h" & "SpielerNameGenerator.hpp", both dont work, same errors as before :X

I dont get it, how do you include another file? The book that im learning from is just using an "#include" and it somehow works, what am I missing?
Last edited on
but at one point it worked when I included it

It worked when you had only one .ccp file that included SpielerNameGenerator.h. As soon as your created the second .cpp file that also included SpielerNameGenerator.h, you created multiple definitions of those functions.

Regardless of whether you're including a .cpp file or a .h file, you're still including function definitions more than once. That is what is causing the linker error.

Consider the following:
1
2
3
4
//  foo.h
int foo ()  // function definition (not declaration)
{  // do something 
}


1
2
// filea.cpp
#include "foo.h"  // includes definition of foo  


1
2
// fileb.cpp
#include "foo.h" // Also includes definition of foo 

Linker now complains that foo is defined twice.

It is tempting to to put commonly used function definitions in a header file, but that is not the correct way to do it. You should put only the declaration of a function in the header. Then create a .cpp file that contains the definition of the function.


Last edited on
Topic archived. No new replies allowed.