我有一个带有构造函数的模板类,它只接受一个参数;一个指向第二个模板类型参数的函数的函数指针。并返回第一个模板类型的元素。在C++类构造函数中的函数指针
下面的代码:
cache_controller.hpp:
template<class TYPE, class KEY>
class cache
{
private:
TYPE (*create_func)(KEY);
public:
cache(TYPE (*func)(KEY));
};
extern sf::Texture * create_texture(std::string path);
cache_controller.cpp:
template<class TYPE, class KEY>
cache<TYPE, KEY>::cache(TYPE (*func)(KEY))
{
this->create_func = func;
}
sf::Texture * create_texture(std::string path)
{
sf::Texture * tex_p = new sf::Texture;
tex_p->loadFromFile(path);
return tex_p;
}
测试代码:
cache<sf::Texture *, std::string> tcache(&create_texture);
然而,当我联系我得到这个错误R:
[Linker error] main.cpp:11: undefined reference to `cache<sf::Texture*, std::string>::cache(sf::Texture* (*)(std::string))'
当然,如果存在实现与arbitary键,值和创造功能我洗耳恭听的对象缓存,任何更简单的方法。
请注意,将模板参数以相反方式放置会更常见,类似于std :: map或其他关联容器:'cache'(key = >值) –
leemes
2013-02-09 14:05:54