首页 > 解决方案 > 错误 C2679 二进制“<<”:未找到采用“T”类型右侧操作数的运算符(或没有可接受的转换)

问题描述

我的模板类中的朋友功能有问题。出于某种原因,它不喜欢我试图在运算符重载友元函数中使用类型为 T 的变量这一事实。

#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;
    }

};

这是一个链表类,我需要友元函数运算符重载才能基本上允许我将任何特定的对象列表输出到文本文件中。我希望有人可以帮助我。

标签: c++11templatesoperator-overloadingfriend

解决方案


推荐阅读