PDA

View Full Version : undefined reference on template


EscapeCharacter
11-20-2002, 12:32 AM
im getting this undefined reference on a template class im making and i cant for the life of me figure out why this is happening, perhaps another set of eyes can point in the right direction.

sframe.h:

#ifndef __SFRAME__
#define __SFRAME__
//#include "sobject.h"
#include "smenu.h"
#include "common.h"

/* This will be the main container class
* all top level widgets will only be able to
* be added to this widget
*/

template <class T>
class SFrame{
public:
SFrame();
// virtual ~SFrame();
void AddObject(T *, uint);
void DeleteObject(uint);
void Pack(uint);
private:
T *list;
uint side;
};
#endif


sframe.cpp:

#include "sframe.h"
template <class T>
SFrame<T>::SFrame(){}

template<class T>
void SFrame<T>::AddObject(T *type, uint s){
if(type == NULL){
type = new T();
type->SetNext(NULL);
type->SetPrev(NULL);
Pack(NORTH);
}
}

template<class T>
void SFrame<T>::Pack(uint s){
side = s;
}


main.cpp:

...
#include "smenu.h" // contains the definition of SMenuBar
#include "sframe.h"
...
SFrame<SMenuBar> *test = new SFrame<SMenuBar>; //line 121
...

that last line is where it dies when linking i get:

g++ sobject.o common.o smenu.o sframe.o simage.o planet.o main.o -o thirdconflict -L/usr/X11R6/lib -lX11 -lXpm
main.o: In function `main':
/home/escape/src/ThirdConflict/ThirdConflict-0.0.6/main.cpp:121: undefined reference to `SFrame<SMenuBar>::SFrame(void)'
collect2: ld returned 1 exit status
make: *** [thirdconflict] Error 1

DrPizza
11-20-2002, 01:57 AM
Template implementations must reside in the header.

Unless the template is exported (with the widely unsupported keyword "export"), both its declaration and definition must be available in every translation unit that uses it.

This means putting the definition into the header.

Failure to do so will result in linker errors.

EscapeCharacter
11-20-2002, 03:06 AM
i never knew that thanks. that seemed to clear up that little problem.

kmj
11-20-2002, 11:03 AM
I don't think that's the case for cc; but it seems to be true for every other compiler I've used.

stuka
11-20-2002, 11:59 AM
DrPizza: do you read C/C++ User's Journal? They had a great article on export recently - almost a must-read for fans of templates.

DrPizza
11-20-2002, 12:32 PM
Originally posted by kmj
I don't think that's the case for cc; but it seems to be true for every other compiler I've used.
There are many compilers named cc.

To the best of my knowledge there is precisely one (1) compiler that presently supports the export keyword (and hence separation of templates into a declaration file and a definition file), and that is the Comeau compiler.

Cfront did/does, I think, have some kind of an export model, but not the one dictated by the Standard.

The utility of export is doubtful, in any case. Due to the way in which the standard is written, I suspect there's no portable way of using export anyway.

=========================================

Stuka: I've read the first part (which is available online) but not the second part.

kmj
11-20-2002, 01:13 PM
The cc on SunOS 6 (I think) is the one I was referring to. And I didn't use the export keyword.