template, undefined reference to fnc
Hi, i have a problem with compiling these file:
Code:
/** @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
Code:
/** @file struct.cpp */
#include "struct.h"
template <typename cStruct> int InitLine(cStruct * toStruct)
{
toStruct->first=NULL;
toStruct->last=NULL;
toStruct->top=0;
return 0;
}
...
Code:
/** @file mainwindow.h */
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include "struct.h"
...
#endif // MAINWINDOW_H
and when i call fnc from mainwindow.cpp
the compile output is:
.../mainwindow.cpp:702: undefined reference to `int InitLine<TMyStruct>(TMyStruct*)'
Re: template, undefined reference to fnc
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.