problems linking when inheriting from template specialization

i ran into a problem implementing a hash table, and distilled the problem down to some small classes, but i don't know what's wrong with them. g++ returns 'multiple definition of' errors when i try to link:


main.o: In function `Base<char>::hash(char&)':
main.cpp:(.text+0x0): multiple definition of `Base<char>::hash(char&)'
Inherit.o:Inherit.cpp:(.text+0x0): first defined here
collect2: ld returned 1 exit status


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
/* base.h */                                                                        
                                                                                    
#ifndef BASE                                                                        
#define BASE                                                                        
                                                                                    
template <class T>                                                                  
class Base {                                                                        
  public:                                                                           
  Base(T & something) : something(something) { }                                    
  T something;                                                                      
                                                                                    
  static int hash(T & something) { return 0; }                                      
};                                                                                  
                                                                                    
template <>                                                                         
int Base<char>::hash(char & h) {                                                    
  return 1 + Base<char>::hash(h);                                                   
}                                                                                   
                                                                                    
#endif                                                                              
                                                                                    
/* end base.h */
/* inherit.h */                                                                     
                                                                                    
#ifndef INHER                                                                       
#define INHER                                                                       
                                                                                    
#include "Base.h"

class Inherit : public Base<char> {                                                 
  public:
  Inherit(char & s);                                                                
};                                                                                  
  
#endif                                                                              
                                                                                    
/* end inherit.h */
/* inherit.cpp */                                                                   
                                                                                    
#include "Inherit.h"                                                                

Inherit::Inherit(char & s) : Base<char>(s){ }                                       

/* end inherit.cpp */
/* main.cpp */
                                                                                    
#include "Inherit.h"                                                                

int main() {
  char c = 'h';
  Inherit in(c);
}

/* end main.cpp */
Create a file base.cpp and move your definition of Base<char>::hash into it. My C++ is rusty, but I think that because the function has no uninstantiated template parameters it is treated like a normal function in terms of duplicate definitions. Since main.o and inherit.o are both compiled using base.h, they both have their own definitions of Base<char>::hash, which the compiler disallows at link time.
Topic archived. No new replies allowed.