Paul Manta
Paul Manta

Reputation: 31577

Templates befriending each other

I have a ResourcePtr<T> class template and a ResouceManager<T> template. I want the two to befriend one another. If I do the following I get a compilation error; how can I fix this?

template<class T>
class ResourcePtr
{
    friend class ResourceManager<T>;
};

template<class T>
class ResourceManager
{
    friend class ResourcePtr<T>;
};

error C2059: syntax error : '<'
error C2238: unexpected token(s) preceding ';'

Upvotes: 2

Views: 113

Answers (1)

AProgrammer
AProgrammer

Reputation: 52284

As usual for mutual things: declare one before the definition of the other.

template<class T>
class ResourceManager;

template<class T>
class ResourcePtr
{
    friend class ResourceManager<T>;
};

template<class T>
class ResourceManager
{
    friend class ResourcePtr<T>;
};

Upvotes: 6

Related Questions