【问题标题】:C++: Friend method not accessing nested classC ++:朋友方法不访问嵌套类
【发布时间】:2011-12-11 13:42:39
【问题描述】:

我有以下几点:

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

这很奇怪,因为我在尝试声明它的行上遇到了错误。有谁知道为什么?

【问题讨论】:

  • 我认为这条线行不通——想想你在做什么,将一个对象分配给一个指针。

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


【解决方案1】:

这一行:

olsm<T>::node* rowNode

应该是:

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

【讨论】:

    【解决方案2】:

    olsm&lt;T&gt;::node* 是一个依赖名称(它依赖于模板参数)。你需要写typename olsm&lt;T&gt;::node* 告诉编译器它引用了一个类型(默认情况下,编译器会假设它引用一个成员)。

    请参阅this question 了解更详细的说明。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-06-18
      • 2013-01-23
      • 1970-01-01
      • 2015-03-06
      • 1970-01-01
      • 1970-01-01
      • 2011-03-18
      • 1970-01-01
      相关资源
      最近更新 更多