Problem with template classes
Jul 17, 2010 at 3:10pm UTC
I have this classes:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
class FunctMenuScreen
{
public :
virtual ~FunctMenuScreen() = 0;
virtual void operator ()() = 0;
};
template <class FunctObj>
class FunctMenu : public FunctMenuScreen
{
typedef void (FunctObj::*FunctMenuAction)();
private :
FunctObj* object;
FunctMenuAction action;
public :
FunctMenu(FunctObj*, FunctMenuAction);
~FunctMenu() {};
void operator ()();
};
and in class MainMenu in some function I have this:
FunctMenu<MainMenu>* test = new FunctMenu<MainMenu>(this , &MainMenu::exit);
and linker show this error:
undefined reference to `FunctMenu<MainMenu>::FunctMenu(MainMenu*, void (MainMenu::*)())'
I compile it by GCC C++ Compiler on Linux.
What have I wrong? Please help, thank you
Jul 17, 2010 at 3:12pm UTC
With templates you must keep the function definitions visible everywhere they are used, so you should keep them on the header
Jul 17, 2010 at 3:18pm UTC
That isn't the problem. Of course I have classes FunctMenuScreen and FunctMenu at one header file, their functions definitions in source file. And the same with class MainMenu.
Jul 17, 2010 at 3:27pm UTC
The function definitions of a template class need to be in the header, not the source file.
Jul 17, 2010 at 3:34pm UTC
Thank you. How could I possible forget that!!! Thank you very much.
Topic archived. No new replies allowed.