繁体   English   中英

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

[英]Error with templated function overloading and out of class definition

我正在学习游戏开发,在书中我需要用模板重载一个函数。 原来的功能是

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)

我怎样才能解决这个问题? 谢谢你。

您忘记在类中添加声明:

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);
};

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM