C++:友元方法不访问嵌套类
我有以下内容:
using namespace std;
template<class T> class olsm;
template<class T> istream& operator>>(istream& in, olsm<T>& x);
template<class T> ostream& operator<<(ostream& out, olsm<T>& x);
template <class T>
class olsm {
friend istream& operator>> <> (istream& in, olsm& x);
friend ostream& operator<< <> (ostream& out, olsm& x);
public:
class node {
public:
};
///Other stuff
};
////More stuff
template<class T>
ostream& operator<<(ostream& out, olsm<T>& x) {
olsm<T>::node* rowNode = x;
//Even more stuff!
return out;
}
但是当我尝试编译时,我得到了,
error: 'rowNode' was not declared in this scope
这很奇怪,因为我在尝试声明它的行上收到错误。有谁知道为什么?
I have the following:
using namespace std;
template<class T> class olsm;
template<class T> istream& operator>>(istream& in, olsm<T>& x);
template<class T> ostream& operator<<(ostream& out, olsm<T>& x);
template <class T>
class olsm {
friend istream& operator>> <> (istream& in, olsm& x);
friend ostream& operator<< <> (ostream& out, olsm& x);
public:
class node {
public:
};
///Other stuff
};
////More stuff
template<class T>
ostream& operator<<(ostream& out, olsm<T>& x) {
olsm<T>::node* rowNode = x;
//Even more stuff!
return out;
}
But when I try to compile I get,
error: 'rowNode' was not declared in this scope
which is odd because I get the error on the line I'm trying to declare it at. Does anyone know why?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
olsm::node*
是一个从属名称(它取决于模板参数)。您需要编写typename olsm::node*
来告诉编译器它引用一个类型(默认情况下,编译器将假设它引用一个成员)。请参阅此问题< /a> 以获得更详细的解释。
olsm<T>::node*
is a dependent name (it depends on a template parameter). You need to writetypename olsm<T>::node*
to tell the compiler that it refers to a type (by default, the compiler will assume it refers to a member).See this question for a more detailed explanation.
这一行:
应该是:
This line:
should be: