Hi,
I am new to template programming and the problem I am having, might really be trivial! In any case, I am trying to set up a class template with a specialized function and use it as a base class. Now I have been working on this for a fair amount of time without finding a solution to the compilation error I get.
I divided the class template in two files which are:
>>>> CC.h
When compiling I get the following error from the linker
Building target: test
Invoking: GCC C++ Linker
g++ -o"test" ./src/CC.o ./src/GG.o ./src/test_c.o
./src/test_c.o: In function `CC<1u>::myprint(int)':
/home/workspace/test/Debug/../src/CC.cpp:15: multiple definition of `CC<1u>::myprint(int)'
./src/GG.o:/home/workspace/test/Debug/../src/CC.cpp:15: first defined here
collect2: ld returned 1 exit status
make: *** [test] Error 1
The error goes away if I change the derived class GG and include CC.h in the main only. What am I doing wrong??
You shouldn't include cpps. It will give you trouble with automagic tools.
I think that the problem resides in that void CC<1>::myprint(int); is not an inline or templatized method, so it shouldn't be defined in a header file.
Actually, I used the inclusion method described in Vandervoorde's book to compile the code in separate files. Also, if I eliminate the inclusion of the .cpp file and copy its content explicitely on the .h file, the code still does not compile.
On the other hand, if I follow ne555 and put the method inline exlicitely, then the code does compile! Still I don't quite understand why. Would you care to elaborate on that? Why one should not (must not??) include non inline, non templatized methods?