简体   繁体   English

相互交友的模板

[英]Templates befriending each other

I have a ResourcePtr<T> class template and a ResouceManager<T> template.我有一个ResourcePtr<T> class 模板和一个ResouceManager<T>模板。 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: '<'错误 C2059:语法错误:'<'
error C2238: unexpected token(s) preceding ';'错误 C2238:“;”之前的意外标记

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM