- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
因此,我得到了一个带有起始代码的任务来实现一个链表(我已经成功地完成了一个未排序的双向链表)并且在给定头文件的起始代码中有一个友元声明似乎有允许我使用 cout
打印链表的目标陈述。这是头文件;请注意,我在私有(private)部分写了所有内容。
#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 data;
node * next;
node * prev;
};
node * head, * tail;
};
#endif // _LINKED_LIST_
在main
,这也是起始代码的一部分,老师写了cout << list;
这让我相信头文件中的 friend 语句的目标是允许列表轻松地打印到控制台。通常我不会在意,但如果我不注释掉 cout << list;
声明然后链接器为我提供以下错误 cout << list;
的每个实例
app.o: In function 'main':
[code directory]/app.cpp:[line the statement is on]: undefined reference to
'operator<<(std::ostream&, LinkedList&)'
我的问题是,friend std::ostream& operator<<(std::ostream& out, LinkedList& list)
是什么意思? cout << list;
的意思和原因导致这个错误?代码在没有语句的情况下执行良好,并且由于我使用教师的 makefile 来组装作业,我认为这不是问题所在。
app.cpp如下
#include <iostream>
#include "linkedlist.h"
using namespace std;
void find(LinkedList& list, char ch)
{
if (list.find(ch))
cout << "found ";
else
cout << "did not find ";
cout << ch << endl;
}
int main()
{
LinkedList list;
list.add('x');
list.add('y');
list.add('z');
cout << list;
find(list, 'y');
list.del('y');
cout << list;
find(list, 'y');
list.del('x');
cout << list;
find(list, 'y');
list.del('z');
cout << list;
find(list, 'y');
return 0;
}
最佳答案
what does
friend std::ostream& operator<<(std::ostream& out, LinkedList& list)
mean
friend declaration声明一个非成员函数,并使其成为该类的友元,这意味着它可以访问 private
和 protected
类(class)成员LinkedList
.
and why does
cout << list;
cause this error?
由于只是声明,所以需要自己定义。这就是您收到 undefined reference 链接器错误的原因。
您可以在类中定义它(并内联定义)
class LinkedList
{
...
friend std::ostream& operator<<(std::ostream& out, LinkedList& list) {
// output something
return out;
}
...
};
或者在类外定义:
std::ostream& operator<<(std::ostream& out, LinkedList& list) {
// output something
return out;
}
顺便说一句:我建议你将第二个参数设为 const LinkedList&
;它不应在 operator<<
内修改.
关于c++ - "friend std::ostream& operator<<(std::ostream& out, LinkedList& list)"是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38093045/
我想添加 LinkedList (我们称之为列表 A)到 LinkedList> (称之为列表 B)。执行此操作后,我需要更改列表 A 的值并将其再次添加到列表 B,但不更改已存储在列表 B 中的值。
更新:感谢所有的回答。我发现的最干净的解决方案是这个: if ( k(Arrays.asList(new LinkedList<>())); 我有一个递归方法,可以从列表中生成所有“n 选 k”组合。
在我的 Java 应用程序中,以下两个都将编译和运行,并产生所需的结果。 //"Rotate" the list items one place to the left. myLinkedList.a
我写了一个LinkedList接受 Nodes 的类存储 Integers . 然后我创建了一个 LinkedList stack = new LinkedList() ,并添加了 Node s 如果
这个问题在这里已经有了答案: What does it mean to "program to an interface"? (33 个答案) 关闭 9 年前。 新手 Java 问题: 谁能解释一下
我有一个问题。我无法并排输出我的 LinkedList。我问了这个问题,但遗憾的是我的老师告诉我不要更改方法头或使用 java 库(如日历)。我得到了很多关于使用它或更改方法头的建议。我是根据年级而定
这里有什么问题?。我正在尝试使用邻接列表,通过利用 util 包中的集合来实现图形数据结构。这里 LinkedList array which holds some integer. Each ele
这个问题已经有答案了: Reversing a linked list in Java, recursively (33 个回答) 已关闭10 年前。 如何使用 linkedList 类中的方法以相反
我需要实现一个 LinkedList,到目前为止,我已经编写了在列表中按顺序插入值的方法。我有我的节点 front 作为我的类的实例数据,当创建我的第一个值并尝试将 front 的 next 值设置为
目前,我的 LinkedList(不是 Java 的)类中有一个方法,可以将单个节点添加到 LinkedList 中,如下所示: public void add(int index, T v) {
我正在编写一个读取 XML 文件的类,该 XML 使用“sax”类进行解析。在我的 XML 文件中,我创建了“for”标签和“宏”,使 for 循环能够写入 XML,例如: Th
我正在处理一个 C++ 作业,我将在一个链表的链表上创建一个搜索引擎。根据要求,我不能使用其他库和 STL。 基本上它会是这样的(我从小列表中删除了变量,因为它们是不相关的): 我的结构是这些: st
老实说,我现在真的很困惑这个问题,并且真的不知道如何解决这个问题。我需要编写一个方法,其中给定一个字符链接列表(例如:{'a','A','d','X'})并返回仅包含大写字符的列表(返回:{'A','
我正在尝试获取可执行文件中的两个链表,并在交替位置将它们合并到一起。前任。 ListOne 1,2,3 和 ListTwo 4,5 新的 ListOne 应该是 1,4,2,5,3。 链表.h文件:
这个问题在这里已经有了答案: Is List a subclass of List? Why are Java generics not implicitly polymorphic? (19 个回答
在尝试了解如何将哈希表插入LinkedLists时,我遇到了麻烦。我失去了尝试过的不同事物的数量。我知道我可以使用ArrayList或其他东西,但是我想使它与LinkedLists一起工作,以便可以对
我一直在尝试编写一种方法,不仅可以从 LinkedList(allUsers) 中删除对象(User),还可以从所有用户拥有的单个 LinkedList 中删除。谁能向我解释为什么这是错误的?我已经包
我有一个列表结构和一个名为树的递归函数。在下面的代码中,它永远不会到达 current == null 语句,因此它将永远运行。 如果我无法使用null,解决方案是什么? private void t
这个问题在这里已经有了答案: How does one add a LinkedList to a LinkedList in C#? (3 个答案) 关闭 9 年前。 假设我有以下内容: Link
我正在尝试为 LinkedList 创建一个反向 ListIterator,并且打算将其实现为 linkedList.listIterator(linkedList. size()) 交换了 next
我是一名优秀的程序员,十分优秀!