与其他模板类中的所有模板类成为朋友
看下面的代码:
template <typename T, int d>
class Grid {
//Following line is what I need to change
template<int d2> friend class Iterator<T,d,d2>;
}
template <typename T, int d, int d2>
class Iterator{
//some code that use private fields of Grid<T,d>
}
template <typename T, int d>
class Iterator<T,d,0>{
//This specialized class also need to use private parts of Grid<T,d>
}
专用和非专用迭代器都应该有权访问私有部分。 行:
template<int d2> friend class Iterator<T,d,d2>;
编译时出现错误:部分专业化“迭代器”声明的朋友
有人知道如何替换它吗?
编辑: 感谢@Xeo 评论,我能够做出解决方法:
template<typename TT, int dd, int d2> friend class Iterator;
但是,这使朋友可以访问所有迭代器模板,而不仅仅是那些具有匹配的第一个和第二个模板参数的模板。
Look at following code:
template <typename T, int d>
class Grid {
//Following line is what I need to change
template<int d2> friend class Iterator<T,d,d2>;
}
template <typename T, int d, int d2>
class Iterator{
//some code that use private fields of Grid<T,d>
}
template <typename T, int d>
class Iterator<T,d,0>{
//This specialized class also need to use private parts of Grid<T,d>
}
Both specialized and not specialized Iterator should have access to private parts.
Line:
template<int d2> friend class Iterator<T,d,d2>;
does not compile with error: partial specialization `Iterator' declared friend
Does anybody know how to replace it?
EDIT:
Thanks to @Xeo comment i was able to make a workaround:
template<typename TT, int dd, int d2> friend class Iterator;
However this gives friend access to all Iterator templates not only to those one that have matching first and second template parameter.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这只是一个解决方法,但它正在起作用:
This is just a workaround but it is working: