Почему я получаю ошибки ссылки в этой программе (с gcc 4.6.2):
#include <iostream>
// prints something;
// the template doesn't add anything
template <typename T>
struct Printer
{
void print()
{
std::cout << "Printer::print" << std::endl;
}
};
// this is an actual template
// calls the method indicated by the second template argument
// belonging to the class indicated by the first template argument
template < typename U, void(U::*func)()>
struct Caller
{
void call(U obj)
{
(obj.*func)();
}
};
// just a wrapper
template<typename V>
struct Wrapper
{
void call_caller_on_printer()
{
Printer<int> a_printer;
Caller<Printer<int>, &Printer<int>::print> caller;
caller.call(a_printer);
}
};
int main()
{
Wrapper<int> the_wrapper;
the_wrapper.call_caller_on_printer();
return 0;
}
Компоновщик жалуется, что Printer :: print является неопределенной ссылкой.Однако, если вы сделаете Wrapper не-шаблоном (шаблон там ничего не добавляет), это сработает.Метод печати Принтера, похоже, не реализован.Почему это так?