简体   繁体   English

如何在 c++ 中做正确的模板输出

[英]How to do right template outputs in c++

i have this error in MSVS project, in this part of code我在 MSVS 项目中有这个错误,在这部分代码中

ostream& getStream(ostream& out)
    {
        for (Node* temp = head; temp; temp = temp->next)
            out << temp->info << " ";
        return out;
    }

Error C2679 binary"<<": no operator was found that accepts the right operand of type " T " (or there is no acceptable conversion)错误 C2679 二进制“<<”:未找到接受类型“T”的正确操作数的运算符(或没有可接受的转换)

#pragma once
#ifndef _LIST_H_
#define _LIST_H_
#include <iostream>
using namespace std;

template <class T>
class MyList
{
protected :
    struct Node
    {
        T info;
        Node* next;
        Node(T info, Node* next = 0) :
            info(info),
            next(next)
        {}
    };

    Node* head;
    ostream& getStream(ostream& out)
    {
        for (Node* temp = head; temp; temp = temp->next)
            out << temp->info << " ";
        return out;
    }
public:
    MyList();
    ~MyList();

    template <class T1>
    friend ostream& operator<<(ostream& os, MyList<T1>& list);

    void Insert(T info, int index);
    int Count();
    void Remove(int index);
    void PopFront();
    void PushFront(T Data);
    void PopBack();
    void PushBack(T Data);
};

template <class T>
MyList<T>::MyList():
    head(0)
{}

template <class T>
MyList<T>::~MyList()
{
    while (head != NULL)
    {
        Node* help = head->next;
        delete head;
        head = help;
    }
}


template <class T>
ostream& operator<<(ostream& out, MyList<T>& list)
{
    return list.getStream(out);
}

template <class T>
void MyList<T>::Insert(T info, int index)
{
    if (Count() < index || index < 0) throw 1;

    if (index == 0)
    {
        head = new Node(info, head);
        return;
    }
    Node* current = head;
    for (int i = 1; i < index; current = current->next, i++);
    current->next = new Node(info, current->next);
}

template <class T>
void MyList<T>::Remove(int index)
{
    if (Count() <= index || index < 0) throw 1;
    if (index == 0)
    {
        Node* help = head->next;
        delete head;
        head = help;
        return;
    }
    Node* current = head;
    for (int i = 1; i < index; current = current->next, i++);
    Node* help = current->next;
    current->next = help->next;
    delete help;
}

template <class T>
void MyList<T>::PopFront()
{
    if (head == 0) return;
    Node* help = head;
    head = head->next;
    delete help;

}

template<class T>
void MyList<T>::PushFront(T Data)
{
    head = new Node(Data, head);
}

template<class T>
void MyList<T>::PopBack()
{
    if (head == 0)return;

    if (Count() == 1)
    {
        delete head;
        head = 0;
    }

    Node* curr;

    for (curr = head; curr->next->next != 0; curr = curr->next);

    delete curr->next;
    curr->next = 0;
}

template<class T>
void MyList<T>::PushBack(T Data)
{
    Node* curr;
    for (curr = head; curr->next != 0; curr = curr->next);
    curr->next = new Node(Data);
}

template <class T>
int MyList<T>::Count()
{
    int count = 0;
    for (Node* current = head; current != NULL; current = current->next, count++);
    return count;
}



#endif // !_LIST_H_


this is declaration of my class i have no ideas how to fix it, big thank to you if you will try to help me这是我的 class 的声明,我不知道如何解决它,如果你愿意帮助我,非常感谢你
sory if have bad description, im only starting对不起,如果有不好的描述,我才刚刚开始

one of descendant of my class have bad implementation of operator<<, and that class jump me to this problem我的 class 的后代之一对运算符 << 的实现很糟糕,而 class 使我跳转到这个问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM