What does "friend std::ostream& operator<<(std::ostream& out, LinkedList& list)" mean?
<p>So I was given an assignment with starter code to implement a linked list (which I have done with an unsorted doubly linked list successfully) and in the starter code in the given header file there was a friend declaration that seems to have the goal of allowing me to print the linked list using the <code>cout</code> statement. This is the header file; note that I wrote everything in the private section.</p>

<pre><code>#ifndef _LINKED_LIST_
#define _LINKED_LIST_

#include &lt;ostream&gt;

class LinkedList
{
public:
    LinkedList();
    ~LinkedList();

    void add(char ch);
    bool find(char ch);
    bool del(char ch);

    friend std::ostream&amp; operator&lt;&lt;(std::ostream&amp; out, LinkedList&amp; list);

private:
    struct node
    {
        char data;
        node * next;
        node * prev;
    };
    node * head, * tail;
};

#endif // _LINKED_LIST_
</code></pre>

<p>In <code>main</code>, which was also part of the starter code, the teacher wrote <code>cout &lt;&lt; list;</code> which leads me to believe the goal of the friend statement in the header file was to allow list to be printed to the console easily. Normally I wouldn't care but if I don't comment out the <code>cout &lt;&lt; list;</code> statements then the linker gives me the following error for every instance of <code>cout &lt;&lt; list;</code></p>

<pre><code>app.o: In function 'main':
[code directory]/app.cpp:[line the statement is on]: undefined reference to
'operator&lt;&lt;(std::ostream&amp;, LinkedList&amp;)'
</code></pre>

<p><strong>My question is</strong>, what does <code>friend std::ostream&amp; operator&lt;&lt;(std::ostream&amp; out, LinkedList&amp; list)</code> mean and why does <code>cout &lt;&lt; list;</code> cause this error? The code executes fine without the statements and since I'm using the instructor's makefile to assemble the assignment, I figure that isn't the issue.</p>

<p>app.cpp is as follows </p>

<pre><code>#include &lt;iostream&gt;
#include "linkedlist.h"

using namespace std;

void find(LinkedList&amp; list, char ch)
{
    if (list.find(ch))
        cout &lt;&lt; "found ";
    else
        cout &lt;&lt; "did not find ";
    cout &lt;&lt; ch &lt;&lt; endl;
}

int main()
{
    LinkedList  list;

    list.add('x');
    list.add('y');
    list.add('z');
    cout &lt;&lt; list;
    find(list, 'y');

    list.del('y');
    cout &lt;&lt; list;
    find(list, 'y');

    list.del('x');
    cout &lt;&lt; list;
    find(list, 'y');

    list.del('z');
    cout &lt;&lt; list;
    find(list, 'y');

    return 0;
}
</code></pre>