如何使以下代码有效?在编译期间,我收到一个错误,告诉我该searchForResource函数没有返回类型.
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);
};
Run Code Online (Sandbox Code Playgroud)
另外,这是我如何定义searchForResource函数?
template<class t>
std::list<typename ResourceManager<T>::ResourceWrapper*>::iterator
ResourceManager<T>::searchForResource(const std::string& file)
{
// ...
}
Run Code Online (Sandbox Code Playgroud)
std::list<ResourceWrapper*>::iterator编译器难以理解.typename在实现和声明中加上前缀,让编译器知道它是一个类型.
像这样:
typename std::list<ResourceWrapper*>::iterator searchForResource(const std::string& file);
Run Code Online (Sandbox Code Playgroud)
template<class T>
class ResourceManager
{
private:
struct ResourceWrapper;
std::list<ResourceWrapper*> resources_;
// | typename lost here
// V
typename std::list<ResourceWrapper*>::iterator
searchForResource(const std::string& file);
};
template<class T>
// | typename lost here asterisk lost here |
// V V
typename std::list<typename ResourceManager<T>::ResourceWrapper*>::iterator
ResourceManager<T>::searchForResource(const std::string& file)
{
return ...
}
Run Code Online (Sandbox Code Playgroud)