C++:友元方法不访问嵌套类

发布于 2024-12-11 03:02:32 字数 1044 浏览 0 评论 0原文

我有以下内容:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

爱殇璃 2024-12-18 03:02:32

olsm::node* 是一个从属名称(它取决于模板参数)。您需要编写 typename olsm::node* 来告诉编译器它引用一个类型(默认情况下,编译器将假设它引用一个成员)。

请参阅此问题< /a> 以获得更详细的解释。

olsm<T>::node* is a dependent name (it depends on a template parameter). You need to write typename 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.

Smile简单爱 2024-12-18 03:02:32

这一行:

olsm<T>::node* rowNode

应该是:

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

This line:

olsm<T>::node* rowNode

should be:

   typename olsm<T>::node* rowNode
// ^^^^^^^^  You need to specify the member is a typename.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文