-
Templates & Linkers
I'm self-teaching Templates, and I think I've gotten a grasp on the concept (doesn't seem too, too tricky) but the problem lies when I try to compile, I'm trying this, that and the other thing, and came across what I believe to be a linking problem, as this code is completely functional when it's all in the same source file.
But the moment I break it up into it's components below, I get compiler errors. Code and errors follow.
SomeClass.h
Code:
template <class newType>
class SomeClass
{
newType * obj;
public:
SomeClass( void );
~SomeClass( void );
};
SomeClass.cpp
Code:
#include <iostream>
#include "SomeClass.h"
template <class newType>
SomeClass<newType>::SomeClass( void )
{
obj = new newType;
}
template <class newType>
SomeClass<newType>::~SomeClass( void )
{
if ( !obj )
delete obj;
}
SCMain.cpp
Code:
#include <iostream>
#include "SomeClass.h"
int main ( )
{
SomeClass<char> * tsc = new SomeClass<char>();
delete tsc;
}
Compiler
Code:
luser@servar:~/> c++ -o main SCMain.cpp SomeClass.cpp
/tmp/ccUiG2LS.o: In function `main':
SCMain.cpp:(.text+0x9a): undefined reference to `SomeClass<char>::SomeClass()'
SCMain.cpp:(.text+0xd8): undefined reference to `SomeClass<char>::~SomeClass()
collect2: ld returned 1 exit status
I do recognize that I'm allocating memory only to delete it, this is a trial for something larger I intend to write, should I get it working, but I was never given a good reference on linkers (and how they work) Any pointers or assistance given would be most appreceated.
-
template class implementation should be done in the header file for most now-days compilers
-
-
Resolved. (and I've bookmarked that FAQ) Thanks guys.