C ++模板:返回list :: iterator [英] C++ templates: Returning list::iterator

查看:224
本文介绍了C ++模板:返回list :: iterator的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使以下代码工作?在编译期间,我得到一个错误,告诉我 searchForResource 函数没有返回类型。

How can I make the following code work? During compilation I get an error telling me that the searchForResource function has no return type.

template<class T>
class ResourceManager
{
  private:
    struct ResourceWrapper;
    std::list<ResourceWrapper*> resources_; // This compiles fine

    std::list<ResourceWrapper*>::iterator  // Error occurs here
        searchForResource(const std::string& file);
};

此外,这是我如何定义 searchForResource function?

Also, is this how I would define the searchForResource function?

template<class t>
std::list<typename ResourceManager<T>::ResourceWrapper*>::iterator
    ResourceManager<T>::searchForResource(const std::string& file)
{
    // ...
}


推荐答案

std :: list :: iterator很难让编译器理解。

std::list::iterator is hard for the compiler to understand. Prefix it with 'typename' in both the implementation and the declaration to let the compiler know that it's a type.

像这样:

typename std::list<ResourceWrapper*>::iterator searchForResource(const std::string& file);

这篇关于C ++模板:返回list :: iterator的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆