Error C2679 binary '<<': no operator found which takes a right-hand operand of type 'T' (or there is no acceptable conversion)

核能气质少年 提交于 2019-12-24 07:29:32

问题


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.

来源:https://stackoverflow.com/questions/55652315/error-c2679-binary-no-operator-found-which-takes-a-right-hand-operand-of-t

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!