c++ - 如何在 C++ 中进行正确的模板输出
问题描述
我在 MSVS 项目中有这个错误,在这部分代码中
ostream& getStream(ostream& out)
{
for (Node* temp = head; temp; temp = temp->next)
out << temp->info << " ";
return out;
}
错误 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_
这是我班级的声明,我不知道如何解决它,非常感谢你,如果你能帮助我
,如果描述不好,我才开始
解决方案
我班的一个后裔对operator<<的实现不好,那个班让我遇到了这个问题
推荐阅读
- kotlin - 掌握 Kotlin 代表的窍门
- sql - Postgresql 排除基于子查询的结果
- javascript - 在 React 中访问第二层数组
- python-3.x - 将数据透视表转换为 Python 数据框
- google-sheets - Google 表格和 ImportXML - 缩小结果范围
- python - Pandas,遍历数据框项并基于列值构建新的数据框
- bash - Bash:如何存储在变量中显示带有 \n 的变量的命令
- java - Maven 测试在 Azure DevOps 中失败。java.io.FileNotFoundException:TestSuiteList.xls(没有这样的文件或目录)
- azure - Redis 锁是否应该在单独的实例上运行?
- javascript - 在 JavaScript 中,我们可以在标签中导入一个 JS 模块而不加载外部文件吗?