PDA

View Full Version : template, undefined reference to fnc



Lodhart
26th October 2009, 12:15
Hi, i have a problem with compiling these file:



/** @file struct.h */

#ifndef STRUCT_H
#define STRUCT_H

using namespace std;

...

template <typename cStruct> int InitLine(cStruct * toStruct);
template <typename cStruct, typename cItem> int AddToLine(cStruct * toStruct, cItem * newItem);
template <typename cStruct, typename cItem> int DeleteFromLine(cStruct * toStruct, cItem * delItem);

#endif // STRUCT_H




/** @file struct.cpp */

#include "struct.h"

template <typename cStruct> int InitLine(cStruct * toStruct)
{
toStruct->first=NULL;
toStruct->last=NULL;
toStruct->top=0;

return 0;
}
...




/** @file mainwindow.h */

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include "struct.h"

...

#endif // MAINWINDOW_H


and when i call fnc from mainwindow.cpp

InitLine(NewLine);

the compile output is:
.../mainwindow.cpp:702: undefined reference to `int InitLine<TMyStruct>(TMyStruct*)'

rknowles
26th October 2009, 18:11
A template implementation must be visible at compile time. The code you show as "struct.cpp" should be part of "struct.h", not in its own file. Then when "mainwindow" is compiled, the template will be found.