我的数据结构老师给了我们手动实现列表和堆栈的任务,它工作得很好,但是我们不得不重新实现它以使用模板接受任何类型的参数。从那时起,我一直面临着许多不同的错误。现在,我有一个c2019错误,但我看不出我的代码有什么问题。
试过这个:pilha.getTopo<int>();
,这个:Lista<int> lista;
,这个:typedef Lista<int> Lista_int;
,但没有运气。
现在,我的代码:
######更新:以下代码正在运行Listas.h
#pragma once
#include "stdafx.h"
#include <iostream>
using namespace std;
/*============================Node================================*/
template <typename N>
class Node
{
N value;
Node *next, *prev;
public:
Node(void) {};
Node(N _value){
this->value = _value;
this->prev = NULL;
this->next = NULL;
};
~Node(void) {};
void setValue(int _value) { this->value = _value; }
void setNext(Node *_next) { this->next = _next; }
void setPrev(Node *_prev) { this->previous = _prev; }
int getValue() { return this->value; }
Node *getNext() { return this->next; }
Node *getPrev() { return this->prev; }
};
/*===========================List===============================*/
template <typename T>
class List
{
Node<T> *begin, *end;
int list_size;
public:
List(void){
this->begin = NULL;
this->end = NULL;
this->list_size = 0;
};
~List(void) {};
template <typename T> void insertBegin(Node<T> node){
Node<T> *newNode = new Node<T>;
*newNode = node;
if(this->list_size == 0){
this->begin = this->end = newNode;
this->list_size += 1;
}else{
newNode->setNext(begin);
this->begin = newNode;
this->list_size += 1;
}
};
template <typename T> void removeBegin(Node<T> node){
Node<T> *newNode = new Node<T>;
*newNode = node;
if(this->list_size == 0){
this->begin = this->end = NULL;
this->list_size = 0;
}else{
if(begin == end)
this->end = NULL;
this->begin = newNode->getNext();
newNode = begin;
newNode->setPrev(NULL);
this->list_size -= 1;
}
};
template <typename T> void insertEnd(Node<T> node){
Node<T> *newNode = new Node<T>;
*newNode = node;
if(this->list_size == 0){
this->begin = this->end = newNode;
this->list_size += 1;
}else{
newNode->setPrev(end);
this->end = newNode;
this->list_size += 1;
}
};
template <typename T> void removeEnd(Node<T> node){
Node<T> *newNode = new Node<T>;
*newNode = node;
if(this->list_size == 0){
this->begin = this->end = NULL;
this->list_size = 0;
}else{
if(begin == end)
this->end = NULL;
this->end = newNode->getPrev();
newNode = end;
newNode->setNext(NULL);
this->list_size -= 1;
}
};
template <typename T> void exibeList(){
Node<T> *node;
cout << "Begin: " << begin << endl
<< "End: " << end << endl
<< "Size: " << list_size << endl << endl;
if(begin != NULL){
node = begin;
do{
cout << "Mem. adress: " << &node << endl
<< "Value: " << node->getValue() << endl
<< "Previous: " << node->getPrev() << endl
<< "Next: " << node->getNext() << endl
<< endl;
node = node->getNext();
}while(node != NULL);
}
};
};
/*===========================Stack==============================*/
template <typename T>
class MyStack
//: public List<T>
{
Node<T> *top, *next;
int my_stack_size;
public:
MyStack<T>(void){
this->my_stack_size = 0;
this->top = NULL;
this->next = NULL;
};
~MyStack<T>(void) {};
template <typename T> void insertTop(Node<T> node){
Node<T> *newNode = new Node<T>;
*newNode = node;
if(this->my_stack_size == 0){
this->top = this->next = newNode;
this->my_stack_size += 1;
}else{
newNode->setNext(top);
this->next = top;
this->top = newNode;
this->my_stack_size += 1;
}
};
template <typename T> void removeTop(){
Node<T> *node;
if(this->my_stack_size > 0){
node = top;
this->top = next;
delete node;
this->my_stack_size -= 1;
}else
cout << "Stack underflow." << endl;
};
template <typename T> void getTop(){
Node<T> *node = new Node<T>;
node = top;
if(node->getPrev() == NULL)
cout << node->getValue() << endl;
else
cout << "Error. Node isn't the first." << endl;
};
template <typename T> void show(){
Node<T> *node;
cout << "Top: " << top << endl
<< "Next: " << next << endl
<< "Size: " << my_stack_size << endl << endl;
if(top != NULL){
node = top;
do{
cout << "Mem. adress: " << &node << endl
<< "Value: " << node->getValue() << endl
<< "Previous: " << node->getPrev() << endl
<< "Next: " << node->getNext() << endl
<< endl;
node = node->getNext();
}while(node != NULL);
}
};
};
最后,Lista.cpp
// Lista.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "Listas.h"
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
/* ERROR C2019
//typedef Pilha<int> Pilha_int;
//typedef Nodo<int> Nodo_int;
//
//Lista_int lista;
//Pilha_int pilha;
//Nodo_int nodo(25), nodo2(40), nodo3(55), nodo4(70);
*/
/* C2019
Pilha<int> pilha;
Nodo<int> nodo(25), nodo2(40);
/*error C2955: use of class template requires template argument list
//Pilha pilha;
//Nodo nodo(25), nodo2(40), nodo3(55), nodo4(70);
pilha.insereTopo(nodo);
pilha.getTopo(); //C2783
pilha.insereTopo(nodo2);
pilha.getTopo(); //C2783
pilha.exibe(); //error C2783: could not deduce template argument for 'T'
pilha.exibe<int>(); //C2019
system("pause");
return 0;
}
提前致谢。