我有一個帶有構造函數的模板類,它只接受一個參數;一個指向第二個模板類型參數的函數的函數指針。並返回第一個模板類型的元素。在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