ld: duplicate symbol

Nov 18, 2012 at 5:32pm
I have 3 header files, x_tuple.h, node.h, rbtree.h. The three compile fine together but when I compile them with rbtree.cpp and main.cpp I get:

ld: duplicate symbol x_tuple::x_tuple()in /var/folders/we/weWPUgg0F3OSCT+uhlIc0k+++TI/-Tmp-//ccP5GVef.o and /var/folders/we/weWPUgg0F3OSCT+uhlIc0k+++TI/-Tmp-//ccPOMM2C.o

x_tuple is only defined in x_tuple.h, though.

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 X_TUPLE_H
#define X_TUPLE_H

#include <iostream>
using namespace std;

struct x_tuple{

	public:
		int key;
		int value;
		int* record;
		
		x_tuple();
		~x_tuple();
		x_tuple(x_tuple &x);
		x_tuple& operator=(x_tuple &x);
	
};

x_tuple::x_tuple(){

	key = -1;
	record = NULL;
	
}

x_tuple::~x_tuple(){

	delete record;

}

x_tuple::x_tuple(x_tuple &x){

	key = x.key;
	value = x.value;
	record = x.record;
	
}

x_tuple& x_tuple::operator=(x_tuple &x){

	key = x.key;
	value = x.value;
	record = x.record;
	
	return *this;

}

#endif 


I don't even know what I should provide for you guys, this seems like a basic problem!
Nov 18, 2012 at 6:18pm
x_tuple is only defined in x_tuple.h, though.

No. Are you sure you know what #include does?
Nov 18, 2012 at 7:36pm
It includes a library, in this case meaning definitions of my constructors and declarations of my variables. Is this not true?

node.h has:
 
#include "x_tuple.h" 


rbtree.h has:
 
#include "node.h" 


main.cpp and rbtree.cpp have:
 
#include "rbtree.h" 
Last edited on Nov 18, 2012 at 7:39pm
Nov 18, 2012 at 7:41pm
An #include directive is simply replaced with the contents of the specified file.
That means you now have two definitions of each x_tuple member function: one in main.cpp and one in rbtree.cpp.
Move the definitions inside the class declaration (which makes the compiler treat them as if they were declared inline, thus not subjecting them to the one definition rule) or move them into their own .cpp file.
Nov 18, 2012 at 7:45pm
To follow up Athar, #include absolutely does not include a library. A library is an already compiled object file (or nicely packaged set of them) which gets linked to at link time by the linker.

It is well worth taking ten minutes to read up on what a compiler does, what a linker does, and how the two work together. Really, really worth it.
Nov 18, 2012 at 8:18pm
Thanks for that explanation.. it makes a lot of sense.

When researching this problem before posting here, I found a similar explanation but still didn't understand, so thank you for that.

edit: wording
Last edited on Nov 18, 2012 at 8:18pm
Topic archived. No new replies allowed.