模板化函数重载和类定义错误

时间:2019-02-12 18:52:37

标签: c++ function class templates overloading

我正在学习游戏开发,在这本书中,我需要使用模板重载函数。原始功能是

template <typename Resource, typename Identifier>
void ResourceHolder<Resource, Identifier>::load(Identifier id, const std::string& filename)
{}

,重载版本为

template <typename Resource, typename Identifier>
template <typename Parameter>
void ResourceHolder<Resource, Identifier>::load(Identifier id, const std::string& filename, const Parameter& secondParam)
{}

当我尝试以此运行main代码时

ResourceHolder<sf::Texture, Textures::ID> textures;

我收到编译错误:

error: prototype for ‘void ResourceHolder<Resource, Identifier>::load(Identifier, const string&, const Parameter&)’ does not match any in class ‘ResourceHolder<Resource, Identifier>’
void ResourceHolder<Resource, Identifier>::load(Identifier id, const std::string& filename, const Parameter& secondParam)

我该如何解决? 谢谢。

1 个答案:

答案 0 :(得分:0)

您忘了在类中添加声明:

template <typename Resource, typename Identifier>
struct ResourceHolder {
    // ...

    // member function must be in the class
    template<typename Parameter>
    void load(Identifier id, const std::string& filename, const Parameter& secondParam);
};