C ++:不访问嵌套类的Friend方法

时间:2011-10-20 07:46:34

标签: c++ compiler-errors operator-overloading friend

我有以下内容:

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

这是奇怪的,因为我在线路上得到错误我试图声明它。有谁知道为什么?

2 个答案:

答案 0 :(得分:9)

olsm<T>::node*是一个依赖名称(它取决于模板参数)。您需要编写typename olsm<T>::node*来告诉编译器它引用了一个类型(默认情况下,编译器会假定它引用了一个成员)。

有关更详细的说明,请参阅this question

答案 1 :(得分:3)

这一行:

olsm<T>::node* rowNode

应该是:

   typename olsm<T>::node* rowNode
// ^^^^^^^^  You need to specify the member is a typename.