小编Leg*_*eth的帖子

"朋友std :: ostream&operator <<(std :: ostream&out,LinkedList&list)"是什么意思?

因此,我获得了一个带有入门代码的作业来实现一个链表(我已经成功完成了未分类的双向链表),并且在给定头文件的入门代码中有一个朋友声明似乎有允许的目标我使用cout语句打印链表.这是头文件; 请注意,我在私人部分写了一切.

#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_
Run Code Online (Sandbox Code Playgroud)

main,这也是初学者代码的一部分,老师写道cout << list;,这让我相信头文件中的友元声明的目标是允许列表轻松地打印到控制台.通常我不在乎,但如果我不注释掉cout << list;语句,那么链接器会为每个实例提供以下错误cout << list;

app.o: In function 'main':
[code directory]/app.cpp:[line the statement is on]: undefined reference to …
Run Code Online (Sandbox Code Playgroud)

c++ friend linker-errors

1
推荐指数
1
解决办法
5924
查看次数

标签 统计

c++ ×1

friend ×1

linker-errors ×1