友元函数无法访问私有结构
friend function can't access private struct
我正试图编写一个friend函数来遍历链表并输出列表中的字符,但由于某些原因,我无法在friend函数中声明Nodes。这是我的代码:
这就是功能:
std::ostream& operator<<(std::ostream& out, LinkedList& list)
{
Node *tempNode = nullptr;
*tempNode = *beginningOfList;
std::cout << "list: ";
while(tempNode)
{
std::cout << tempNode->letter;
if (tempNode->next)
std::cout << ", ";
tempNode = tempNode->next;
}
return out;
}
这是头文件:
#ifndef _LINKED_LIST_
#define _LINKED_LIST_
#include <ostream>
class LinkedList
{
public:
LinkedList();
~LinkedList();
void add(char ch);
bool find(char ch);
bool del(char ch);
friend std::ostream& operator<<(std::ostream& out, LinkedList& list);
private:
struct Node
{
char letter;
Node *next;
};
Node *beginningOfList;
};
#endif // _LINKED_LIST_
当我试图编译它时,我会收到消息"Node未在此范围内声明"、"*tempNode未在此作用域内声明"answers"*beginingOfList未在此作用区内声明"。我猜问题与命名空间有关,但我真的不确定。
这是在说实话。未在该范围中声明节点等。您的运算符是一个全局函数,但这些都在LinkedList的范围内。尝试将它们称为LinkedList::Node、list->beginingOfList等。
相关文章:
- 通过方法访问结构
- 使用不带参数的函数访问结构元素
- 访问存储在向量C++中的结构的多态成员
- 访问类lintalizer列表中的结构元素
- 仅使用结构名称访问结构成员
- 如何从其他结构访问受保护的结构变量
- 如何从类中定义的结构访问私有成员?c++
- 枚举范围无法通过 C++ 中的类中的结构访问
- 通过更高级别的结构访问子变量
- 从匿名结构访问枚举条目
- 如何定义别名以通过结构访问枚举类成员
- C 用于向量中的结构访问索引或使用参考值
- 奇怪的结构访问错误
- 将数组作为结构访问与未定义的行为
- 使用c++以及x86和x64体系结构访问处理器中断
- 使用 ostream 从另一个类的结构访问数据
- 从结构访问数组会使程序崩溃
- 联合中的C++结构访问联合成员
- SWIG如何使用Lua表从Lua中的c结构访问字节数组
- c++结构访问冲突