Paul Manta
Paul Manta

Reputation: 31597

C++: Return pointer to template subclass

What is the syntax for doing something like this? When I try to compile the code below it tells me that a ';' was expected before '*', pointing to the return type of the function, 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)
}

Upvotes: 3

Views: 1550

Answers (1)

Naveen
Naveen

Reputation: 73503

You are missing the typename keyword. See this question for more details on why typename is required. This code should compile:

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

Upvotes: 10

Related Questions