Error C2679 binary '<<': no operator

2019-08-06 05:50发布

问题:

I'm having problems with my friend function within my template class. For some reason it doesn't like the fact that I'm trying to use a variable that is type T in an operator overloading friend function.

#include <iostream>
#include <fstream>
#include <string>

template <typename T>
class LL
{
    struct Node
    {
        T mData;
        Node *mNext;

        Node();
        Node(T data);
    };

private:
    Node *mHead, *mTail;
    int mCount;

public:
    LL();
    ~LL();
    bool insert(T data);
    bool isExist(T data);
    bool remove(T data);
    void showLinkedList();
    void clear();
    int getCount() const;
    bool isEmpty();

    friend std::ofstream& operator<<(std::ofstream& output, const LL& obj)
    {
        Node* tmp;

        if (obj.mHead != NULL)
        {
            tmp = obj.mHead;

            while (tmp != NULL)
            {
                output << tmp->mData << std::endl; // "tmp->mData is causing the error
                tmp = tmp->mNext;
            }
        }

        return output;
    }

};

This is a linked list class, and I need the friend function operator overload to basically allow me to output any particular list of objects onto a text file. I hope someone can help me out.