C ++:返回指向模板子类的指针

时间:2011-04-11 05:57:13

标签: c++ templates

做这样的事情的语法是什么?当我尝试编译下面的代码时,它告诉我a ';' was expected before '*',指向函数的返回类型ResourceManager<T>::ResourceWrapper*

template<class T>
class ResourceManager
{
  private:
    struct ResourceWrapper;
    ResourceWrapper* pushNewResource(const std::string& file);
};

// (Definition of ResourceWrapper not shown.)

template <class T>
ResourceManager<T>::ResourceWrapper* ResourceManager<T>::pushNewResource(
    const std::string& file)
{
    // (Irrelevant details)
}

1 个答案:

答案 0 :(得分:10)

您错过了typename关键字。有关为何需要typename的详细信息,请参阅此question。这段代码应编译:

template<class T>
class ResourceManager
{
  private:
    struct ResourceWrapper;
    ResourceWrapper* pushNewResource(const std::string& file);
};

// (Definition of ResourceWrapper not shown.)

template <class T>
typename ResourceManager<T>::ResourceWrapper* ResourceManager<T>::pushNewResource(
^^^^^^^^    const std::string& file)
{
    // (Irrelevant details)
}