problem with splitting program to header and cpp files

im trying to split a program to 3 different files (header ,cpp and main.cpp).
the program worked when they were all in the same file but now after i splitted them i get this error:


Error 5 error LNK1169: one or more multiply defined symbols found c:\users\al sade\documents\visual studio 2013\Projects\ConsoleApplication15\Debug\ConsoleApplication15.exe 1 1 ConsoleApplication15
Error 1 error LNK2005: "struct node * start" (?start@@3PAUnode@@A) already defined in main.obj c:\Users\Al Sade\documents\visual studio 2013\Projects\ConsoleApplication15\ConsoleApplication15\tms.obj ConsoleApplication15
Error 2 error LNK2005: "struct node * temp1" (?temp1@@3PAUnode@@A) already defined in main.obj c:\Users\Al Sade\documents\visual studio 2013\Projects\ConsoleApplication15\ConsoleApplication15\tms.obj ConsoleApplication15
Error 3 error LNK2005: "struct node * temp2" (?temp2@@3PAUnode@@A) already defined in main.obj c:\Users\Al Sade\documents\visual studio 2013\Projects\ConsoleApplication15\ConsoleApplication15\tms.obj ConsoleApplication15
Error 4 error LNK2005: "struct node * temp3" (?temp3@@3PAUnode@@A) already defined in main.obj c:\Users\Al Sade\documents\visual studio 2013\Projects\ConsoleApplication15\ConsoleApplication15\tms.obj ConsoleApplication15

this is my files:

main.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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
  #include <iostream>
#include "tsm.h"




using namespace std;



int main()
{
	char ch;
	do
	{
		char i;
		cout << "Press 'a' to add node , 'd' to delete" << endl;
		cout << " 's' for search, 'v' for display ,'e' for backward display" << endl;
		cin >> i;
		switch (i)
		{
		case'a':
			addnode();
			break;
		case'd':
			delnode();
			break;
		case'v':
			display();
			break;
		case's':
			search();
			break;
		case'e':
			show();
			break;
		default:
			cout << "Bad input" << endl;
			break;
		}
		cout << "want to process more y/n" << endl;
		cin >> ch;
	} while (ch != 'n');
	return 0;
}


tsm.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
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
#include <iostream>

#ifndef _TSM_H_
#define _TSM_H_
#endif

using namespace std;
#include "tsm.h"


void addnode()          //adding node
{
	char r;
	temp1 = new node;
	cout << "enter int to store" << endl;
	cin >> temp1->data;
	cout << "press 's' to add in start,'m' for midd , 'e' for end" << endl;
	cin >> r;
	switch (r)
	{
	case's':                 //add start
		if (start == NULL)
		{
			start = temp1;
			temp1->next = NULL;
			temp1->prev = NULL;
		}
		else
		{
			temp2 = start;
			temp1->next = temp2;
			temp1->prev = NULL;
			start = temp1;
			temp2->prev = temp1;
		}
		break;
	case'e':               //add end
		if (start == NULL)
		{
			start = temp1;
			temp1->next = NULL;
			temp1->prev = NULL;
		}
		else
		{
			temp2 = start;
			while (temp2->next != NULL)
				temp2 = temp2->next;
			temp2->next = temp1;
			temp1->prev = temp2;
			temp1->next = NULL;
		}
		break;
	case'm':                //add mid
		int num;
		cout << "enter node after which you want to enter" << endl;
		cin >> num;
		temp2 = start;
		for (int i = 0; i<num; i++)
		{
			if (start == NULL)
				cout << "given node not found" << endl;
			else
			{
				temp3 = temp2;
				temp2 = temp2->next;

			}
		}
		temp1->next = temp2;
		temp3->next = temp1;
		temp1->prev = temp3;
		temp2->prev = temp1;
		break;
	}
}
void display()        //displaying
{

	temp3 = start;
	if (start == NULL)
		cout << "no node to display" << endl;
	else
	{
		while (temp3->next != NULL)
		{
			cout << "Data stored is " << temp3->data << " at " << temp3 << endl;
			temp3 = temp3->next;
		}
		cout << "Data stored is " << temp3->data << " at " << temp3 << endl;
	}
}

void search()            //searching
{
	int p;
	cout << "enter no to search" << endl;
	cin >> p;
	temp1 = start;
	while (temp1->next != NULL)
	{
		if (temp1->data == p)
		{
			cout << temp1->data << " is stored in " << temp1->next << endl;
		}
		temp1 = temp1->next;
	}
}
void delnode()           //deleting
{

	char d;
	cout << "press 's' to delete from start,'m' for midd , 'e' for end" << endl;
	cin >> d;
	switch (d)
	{
	case's':               //delete start
		if (start == NULL)
		{
			cout << "no node to delete" << endl;
		}
		else
		{
			temp1 = start;
			start = start->next;
			start->prev = NULL;
			delete temp1;
		}
		break;
	case'e':            //delete end
		if (start == NULL)
		{
			cout << "no node to delete" << endl;
		}
		else
		{
			temp1 = start;
			while (temp1->next != NULL)
			{
				temp2 = temp1;
				temp1 = temp1->next;
			}
			delete temp1;
			temp2->next = NULL;
		}
		break;
	case'm':               //delete mid
		int num;
		cout << "enter node you want to delete" << endl;
		cin >> num;

		temp1 = start;
		for (int i = 1; i<num; i++)
		{
			if (start == NULL)
				cout << "given node does not exist" << endl;
			else
			{
				temp2 = temp1;
				temp1 = temp1->next;
			}
		}
		temp3 = temp1->next;
		temp2->next = temp3;
		temp3->prev = temp2;
		delete temp1;
		break;
	}
}
void show()               //backward display
{
	cout << "backward display" << endl;
	temp3 = start;
	if (start == NULL)
		cout << "no node to display" << endl;
	else
	{
		while (temp3->next != NULL)
		{
			temp3 = temp3->next;
		}
		while (temp3->prev != NULL)
		{
			cout << "Data stored is " << temp3->data << " at " << temp3 << endl;
			temp3 = temp3->prev;
		}
		cout << "Data stored is " << temp3->data << " at " << temp3 << endl;
	}
}



tsm.header:


1
2
3
4
5
6
7
8
9
10
11
12
13
struct node
{
	int data;
	node *next;
	node *prev;
};

void addnode();
void delnode();
void display();
void show();
void search();
node *start = NULL, *temp1, *temp2, *temp3;



i tried to solve it with #ifndef but i just cant get it to work.
any help will be great!
The include guards should be in the header file not the cpp file.
1
2
3
4
#ifndef _TSM_H_
#define _TSM_H_
 .... the rest of the code for the header file goes here
#endif 


Article: http://www.cplusplus.com/articles/Gw6AC542/

i tried to do so, and changed my files to look like this:

main.ccp:

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
#include<iostream>
#include "tsm.h"

using namespace std;


int main()
{
	char ch;
	do
	{
		char i;
		cout << "Press 'a' to add node , 'd' to delete" << endl;
		cout << " 's' for search, 'v' for display ,'e' for backward display" << endl;
		cin >> i;
		switch (i)
		{
		case'a':
			addnode();
			break;
		case'd':
			delnode();
			break;
		case'v':
			display();
			break;
		case's':
			search();
			break;
		case'e':
			show();
			break;
		default:
			cout << "Bad input" << endl;
			break;
		}
		cout << "want to process more y/n" << endl;
		cin >> ch;
	} while (ch != 'n');
	return 0;
}


tsm.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
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
void addnode()          //adding node
{
	char r;
	struct node* temp1 = (struct node*)malloc(sizeof(struct node));
	cout << "enter int to store" << endl;
	cin >> temp1->data;
	cout << "press 's' to add in start,'m' for midd , 'e' for end" << endl;
	cin >> r;
	switch (r)
	{
	case's':                 //add start
		if (start == NULL)
		{
			start = temp1;
			temp1->next = NULL;
			temp1->prev = NULL;
		}
		else
		{
			temp2 = start;
			temp1->next = temp2;
			temp1->prev = NULL;
			start = temp1;
			temp2->prev = temp1;
		}
		break;
	case'e':               //add end
		if (start == NULL)
		{
			start = temp1;
			temp1->next = NULL;
			temp1->prev = NULL;
		}
		else
		{
			temp2 = start;
			while (temp2->next != NULL)
				temp2 = temp2->next;
			temp2->next = temp1;
			temp1->prev = temp2;
			temp1->next = NULL;
		}
		break;
	case'm':                //add mid
		int num;
		cout << "enter node after which you want to enter" << endl;
		cin >> num;
		temp2 = start;
		for (int i = 0; i<num; i++)
		{
			if (start == NULL)
				cout << "given node not found" << endl;
			else
			{
				temp3 = temp2;
				temp2 = temp2->next;

			}
		}
		temp1->next = temp2;
		temp3->next = temp1;
		temp1->prev = temp3;
		temp2->prev = temp1;
		break;
	}
}
void display()        //displaying
{

	temp3 = start;
	if (start == NULL)
		cout << "no node to display" << endl;
	else
	{
		while (temp3->next != NULL)
		{
			cout << "Data stored is " << temp3->data << " at " << temp3 << endl;
			temp3 = temp3->next;
		}
		cout << "Data stored is " << temp3->data << " at " << temp3 << endl;
	}
}

void search()            //searching
{
	int p;
	cout << "enter no to search" << endl;
	cin >> p;
	temp1 = start;
	while (temp1->next != NULL)
	{
		if (temp1->data == p)
		{
			cout << temp1->data << " is stored in " << temp1->next << endl;
		}
		temp1 = temp1->next;
	}
}
void delnode()           //deleting
{

	char d;
	cout << "press 's' to delete from start,'m' for midd , 'e' for end" << endl;
	cin >> d;
	switch (d)
	{
	case's':               //delete start
		if(start==NULL)
	{
							   cout << "no node to delete" << endl;
	}
		  else
		  {
			  temp1 = start;
			  start = start->next;
			  start->prev = NULL;
			  delete temp1;
		  }
		  break;
	case'e':            //delete end
		if(start==NULL)
	{
							cout << "no node to delete" << endl;
	}
		 else
		 {
			 temp1 = start;
			 while (temp1->next != NULL)
			 {
				 temp2 = temp1;
				 temp1 = temp1->next;
			 }
			 delete temp1;
			 temp2->next = NULL;
		 }
		 break;
	case'm':               //delete mid
		int num;
		cout << "enter node you want to delete" << endl;
		cin >> num;

		temp1 = start;
		for (int i = 1; i<num; i++)
		{
			if (start == NULL)
				cout << "given node does not exist" << endl;
			else
			{
				temp2 = temp1;
				temp1 = temp1->next;
			}
		}
		temp3 = temp1->next;
		temp2->next = temp3;
		temp3->prev = temp2;
		delete temp1;
		break;
	}
}
void show()               //backward display
{
	cout << "backward display" << endl;
	temp3 = start;
	if (start == NULL)
		cout << "no node to display" << endl;
	else
	{
		while (temp3->next != NULL)
		{
			temp3 = temp3->next;
		}
		while (temp3->prev != NULL)
		{
			cout << "Data stored is " << temp3->data << " at " << temp3 << endl;
			temp3 = temp3->prev;
		}
		cout << "Data stored is " << temp3->data << " at " << temp3 << endl;
	}
}


tsm.h:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#ifndef _TSM_H_
#define _TSM_H_


struct node
{
	int data;
	node *next;
	node *prev;
};

node *start = NULL, *temp1, *temp2, *temp3;
void addnode();
void delnode();
void display();
void show();
void search();

#endif  


but i still get the errors:

Error 5 error LNK1169: one or more multiply defined symbols found c:\users\al sade\documents\visual studio 2013\Projects\tsmPProject\Debug\tsmPProject.exe 1 1 tsmPProject
Error 1 error LNK2005: "struct node * start" (?start@@3PAUnode@@A) already defined in main.obj c:\Users\Al Sade\documents\visual studio 2013\Projects\tsmPProject\tsmPProject\tsm.obj tsmPProject
Error 2 error LNK2005: "struct node * temp1" (?temp1@@3PAUnode@@A) already defined in main.obj c:\Users\Al Sade\documents\visual studio 2013\Projects\tsmPProject\tsmPProject\tsm.obj tsmPProject
Error 3 error LNK2005: "struct node * temp2" (?temp2@@3PAUnode@@A) already defined in main.obj c:\Users\Al Sade\documents\visual studio 2013\Projects\tsmPProject\tsmPProject\tsm.obj tsmPProject
Error 4 error LNK2005: "struct node * temp3" (?temp3@@3PAUnode@@A) already defined in main.obj c:\Users\Al Sade\documents\visual studio 2013\Projects\tsmPProject\tsmPProject\tsm.obj tsmPProject


any idea why??
Move line 12 of tsm.h to tsm.cpp.

Do not define variables in header files. (And start is the only variable that should be defined. The others should be local to the functions that use them.)

#include "filename" is just textual inclusion of a file. It's as if you copy and pasted the file into the one that is including it. So, if you have variable definitions in a header file and you include it in more than one source file, you have multiple definitions of the same variable.
Topic archived. No new replies allowed.