我需要使用模板构建链表,但我不知道为什么不起作用,我以前构建过链表,但从未使用模板。现在我的问题是,如果我创建列表,一切正常,但是当我尝试向其中插入内容时,出现以下错误:
Error C2664 'Nodo<D>::Nodo(Nodo<D> &&)': cannot convert argument 1 from 'const int' to 'const Nodo<D> &' Datos2 d:\google drive\visual studio 2015\projects\datos2\datos2\listaSimple.h 69
Error C2664 'Nodo<D>::Nodo(Nodo<D> &&)': cannot convert argument 1 from 'const int' to 'const Nodo<D> &' Datos2 d:\google drive\visual studio 2015\projects\datos2\datos2\listaSimple.h 73
使用我的以下代码:
//linkedList.h
#pragma once
#ifndef _LISTASIMPLE_H
#define _LISTASIMPLE_H
template<class D>
struct Nodo
{
int carga;
int binario;
D caracter;
Nodo<D> *Siguiente;//means next
};
template<class D>
class listaSimple
{
public:
listaSimple();
~listaSimple();
void InsertarInicio(const D&);
bool ListaVacia();
void Mostrar();
private:
Nodo<D> *primero;
Nodo<D> *ultimo;
};
template<class D>
listaSimple<D>::listaSimple()
{
primero = NULL;
}
template<class D>
listaSimple<D>::~listaSimple()
{
Nodo<D> *aux;
while (primero != NULL)
{
aux = primero;
primero = primero->Siguiente;
delete aux;
}
}
template<class D>
void listaSimple<D>::InsertarInicio(const D& dato)
{
if (ListaVacia())
{
primero = new Nodo<D>(dato);
}
else
{
Nodo<D> *nodoNuevo = new Nodo<D>(dato);
nodoNuevo->Siguiente = primero;
primero = nodoNuevo;
}
}
template<class D>
bool listaSimple<D>::ListaVacia()
{
if (primero == NULL)
{
return true;
}
else
{
return false;
}
}
template<class D>
inline
void listaSimple<D>::Mostrar()
{
Nodo<D> *aux = primero;
while (aux != NULL)
{
cout << aux->caracter << "->";
aux = aux->Siguiente;
}
}
和
//Source.cpp
#include <iostream>
#include <string>
#include "linkedList.h"
using namespace std;
int main() {
listaSimple<int> Nueva;
Nueva.InsertarInicio(5);
system("pause");
return 0;
}
最佳答案
请参阅 Node
的更正版本和 linkedList
.注意 Node
和 linkedList
不包含有关实际数据的任何信息。事实上,您可以在最后声明数据 ( struct MyData
)。
为了打印,我添加了一个函数:
node->data.print();
这边Node
和 linkedList
不直接负责打印数据,他们不需要了解数据的任何信息。他们可以问DataType
打印数据。 DataType
必须包含 print
打印自己的内容的功能。
template<typename DataType>
struct Node
{
DataType data;
Node<DataType> *Next;
Node()
{
Next = nullptr;
}
};
template<typename DataType>
class linkedList
{
public:
linkedList()
{
first = NULL;
}
~linkedList()
{
Node<DataType> *aux;
while (first != NULL)
{
aux = first;
first = first->Next;
delete aux;
}
}
void InsertBegining(const DataType& data)
{
Node<DataType> *newNode = new Node<DataType>;
newNode->data = data;
if (first)
{
newNode->Next = first;
first = newNode;
}
first = newNode; //<== you forgot this
}
void Print()
{
Node<DataType> *walk = first;
while (walk)
{
walk->data.print();
walk = walk->Next;
}
}
private:
Node<DataType> *first;
};
现在你可以声明MyData
并使用它。确保 MyData
包括 print
功能。还有 MyData
由于分配数据的方式,必须是 POD(普通旧数据,它不能包含指针)。
int main()
{
struct MyData
{
int charge;
int binario;
char ch;
void print()
{
cout << charge << ", " << binario << ", " << ch << "\n";
}
};
linkedList<MyData> list;
MyData data;
data.binario = 1;
data.ch = 'A';
data.charge = 10;
list.InsertBegining(data);
data.binario = 2;
data.ch = 'B';
data.charge = 20;
list.InsertBegining(data);
list.Print();
system("pause");
return 0;
}
另一种方法:
您可以添加 <<
MyData
的运算符重载
struct MyData
{
int charge;
int binario;
char ch;
friend std::ostream& operator<< (std::ostream &out, MyData &x)
{
out << x.ch << ", " << x.binario << ", " << x.charge;
return out;
}
};
所以 MyData
知道如何打印自己。示例:
MyData data;
data.ch = 'A';
data.binario = 1;
data.charge = 10;
cout << data << "\n";
这应该打印 "A, 1, 10"
.
然后你可以改变linkList::Print()
到
...
void Print()
{
Node<DataType> *walk = first;
while (walk)
{
std::cout << walk->data << "\n";
walk = walk->Next;
}
}
现在linkedList
独立于MyData
只要MyData
有<<
运算符重载(其数据为 POD)。您还可以将此链表用于基本类型。示例:
linkedList<int> test;
test.InsertBegining(1);
test.InsertBegining(2);
test.Print();
关于c++ - 带模板的链表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40443721/