LNK2005

Trying to brush up on my C++ but I'm having some link errors. Suggestions welcomed :p

Errors:
Error 1 error LNK2005: "void __cdecl chooserand(void)" (?chooserand@@YAXXZ) already defined in main.obj C:\Users\dfurball\Documents\Visual Studio 2010\Projects\day1\day1\state.obj
Error 2 error LNK2005: _main already defined in main.obj C:\Users\dfurball\Documents\Visual Studio 2010\Projects\day1\day1\state.obj
Error 3 error LNK2005: "char state" (?state@@3DA) already defined in main.obj C:\Users\dfurball\Documents\Visual Studio 2010\Projects\day1\day1\state.obj
Error 4 error LNK2005: "int choice" (?choice@@3HA) already defined in main.obj C:\Users\dfurball\Documents\Visual Studio 2010\Projects\day1\day1\state.obj
Error 5 error LNK1169: one or more multiply defined symbols found
C:\Users\dfurball\Documents\Visual Studio 2010\Projects\day1\Debug\day1.exe 1


main.h
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
#ifndef main_H
#define main_H

#include <iostream>
using namespace std;

int choice;
char state;

void chooserand()
{
	cout << endl;
	cout << "Please make a numbered selection:" << endl;
	cout << "1) Archieve" << endl;
	cout << "2) Character Generation" << endl;
	cin >> choice;

	if (choice > 1){ 
		
		cout << endl;
		cout << "1) Type Suggestion" << endl;
		cout << "2) All Random" << endl;
		cin >> choice;

			switch (choice){
			case 1: 
				state = 'A';
			case 2:
				state = 'B';
			default:
				cout << "Incorrect STATE" << endl;
			}

	}
	else{
		cout << endl;
		cout << "Welcome to the Archieves, please choose a mode:" << endl;
		cout << "1) Character Input" << endl;
		cout << "2) Browse the Archieve" << endl;
		cin >> choice;
	
			switch (choice){
			case 1: 
				state = 'C';
			case 2:
				state = 'D';
			default:
				cout << "Incorrect STATE" << endl;
			}
	}
}
#endif 


main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include "main.h"


void main()
{
	cout << "Please indicate system selection #:" << endl;
	cout << "1) Lucid Gaming System" <<endl;
	cout << "2) Legacy Crossing" <<endl;
	
	cin >> choice;
	cout << endl;

	 cout << "Thank you for choosing ";
	 if (choice > 1){
		 cout << "Legacy Crossing" << endl;
		chooserand();
	 }
	 else{
		 cout << "Lucid Gaming" << endl;
		 chooserand();
	 }
}


state.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#ifndef state_H
#define state_H

void main()
{

}

void templ() // Output character template
{
	cout << "Template INSERT here" << endl;
}

#endif 


state.cpp
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
#include "main.h"
#include "state.h"



void pkstate()
{
	if (state = 'A'){ //type suggestion
		cout << "Please insert Type suggestion:" <<endl;
		cout << "(type list to view possibilities)" << endl;
		cout << endl;
	}
	if (state = 'B'){ // all random

	}
	if (state = 'C'){ // Archieve INPUT
		templ();
	}
	if (state = 'D'){ // Archieve OUTPUT
	
	}
	else{

	}


}
The compiler takes state.cpp (which includes main.h) and main.cpp (which includes the exact same main.h) and turns them into two separate object files. The linker then has to link together these object files to make the final program.

Each of these object files contains a definition of the functions chooserand and main, and the variables state and choice.

When you have functions and variables in the same namespace with identical names, how can the linker tell them apart? It cannot, so it does not know which one to use, so it complains. This is why we do not put function defintions in header files. We only put function prototypes in header files. If the function defintions were in the cpp file, they would exist only in that cpp file, and thus only in one object file, so the linker would know which one to use.

Why do you have a function main in state.h? That makes no sense at all. Note that in C++ main returns an int. void main() is in conflict with the standard.
Last edited on
Thank you, this resolved my problems.
No idea why I named it main o.0

Moved -> chooserand() to main.cpp
Changed state.h -> main() to ostate()
Changed state.cpp -> pkstate() to pkstate(char state)

Thanks for the speedy reply! <3

Topic archived. No new replies allowed.