C ++-链表代码-在此范围内未声明LNode [英] C++ - Linked List code - LNode was not declared in this scope
本文介绍了C ++-链表代码-在此范围内未声明LNode的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在用C ++编写链表的类,并且在写<<的运算符重载时遇到问题.我班的标题是:
I am writing a class for a linked list in C++ and I am having an issue when writing the operator overload for <<. My class' header is:
class LList
{
private:
struct LNode
{
LNode ();
int data;
LNode * next;
};
public:
LList ();
LList (const LList & other);
~LList ();
LList & operator = (const LList & other);
bool operator == (const LList & other);
int Size () const;
friend ostream & operator << (ostream & outs, const LList & L);
bool InsertFirst (const int & value);
bool InsertLast (const int & value);
bool DeleteFirst ();
bool DeleteLast ();
private:
LNode * first;
LNode * last;
int size;
};
而运算符是:
ostream & operator << (ostream & outs, const LList & L){
LNode *np;
np=L.first;
while(np!=NULL){
outs<<np->data;
np=np->next;
}
return outs;
}
编译代码时出现错误:
LList.cpp: In function ‘std::ostream& operator<<(std::ostream&, const LList&)’:
LList.cpp:36:2: error: ‘LNode’ was not declared in this scope
LNode *np;
^
LList.cpp:36:9: error: ‘np’ was not declared in this scope
LNode *np;
我以为我可以在一个朋友函数中实例化一个结构,但这似乎不起作用.你们都知道发生了什么吗?
I thought I could instantiate a struct inside a friend function but it looks like that doesn't work. Any of you know what is happening?
推荐答案
由于您的operator<<
是全局函数,因此需要使用以下命令访问内部类:
Since your operator<<
is a global function, you need to access your inner class with:
LList::LNode *np;
由于此函数是LList
的friend
,因此它可以访问私有的LNode
类.
Since this function is a friend
of LList
, then it can access the private LNode
class.
这篇关于C ++-链表代码-在此范围内未声明LNode的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文