Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 用模板手工实现列表和堆栈_C++_Templates_Stack - Fatal编程技术网

C++ 用模板手工实现列表和堆栈

C++ 用模板手工实现列表和堆栈,c++,templates,stack,C++,Templates,Stack,我的数据结构老师让我们手动实现列表和堆栈,它工作得很好,但是我们必须重新实现它,使用模板接受任何类型的参数。从那以后,我一直面临着许多不同的错误。现在,我有一个c2019错误,但我看不出我的代码有什么问题。 尝试了以下操作:pilha.getTopo(),这是:Lista Lista,这是:typedef Lista Lista_int,但没有运气 现在,我的代码: ######更新:下面的代码正在运行 Listas.h #pragma once #include "stdafx.h" #in

我的数据结构老师让我们手动实现列表和堆栈,它工作得很好,但是我们必须重新实现它,使用模板接受任何类型的参数。从那以后,我一直面临着许多不同的错误。现在,我有一个c2019错误,但我看不出我的代码有什么问题。 尝试了以下操作:
pilha.getTopo(),这是:
Lista Lista,这是:
typedef Lista 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);
        }
    };
};
#pragma一次
#包括“stdafx.h”
#包括
使用名称空间std;
/*=====================================节点================================*/
模板
类节点
{
N值;
节点*next,*prev;
公众:
节点(void){};
节点(N_值){
此->值=\u值;
此->上一个=空;
此->下一步=空;
};
~Node(void){};
void setValue(int _value){this->value=_value;}
void setNext(Node*\u next){this->next=\u next;}
void setPrev(Node*_prev){this->previous=_prev;}
int getValue(){返回此->值;}
Node*getNext(){返回此->下一步;}
Node*getPrev(){返回此->prev;}
};
/*=======================================列表===============================*/
模板
班级名单
{
节点*开始,*结束;
int列表大小;
公众:
名单(作废){
此->开始=空;
此->结束=空;
此->列表大小=0;
};
~List(void){};
模板void insertBegin(节点){
Node*newNode=新节点;
*newNode=节点;
如果(此->列表大小==0){
此->开始=此->结束=新节点;
此->列表大小+=1;
}否则{
新建节点->设置下一步(开始);
此->开始=新节点;
此->列表大小+=1;
}
};
模板无效删除开始(节点){
Node*newNode=新节点;
*newNode=节点;
如果(此->列表大小==0){
此->开始=此->结束=空;
此->列表大小=0;
}否则{
如果(开始==结束)
此->结束=空;
此->开始=新节点->获取下一步();
newNode=begin;
newNode->setPrev(空);
此->列表大小-=1;
}
};
模板无效插入(节点){
Node*newNode=新节点;
*newNode=节点;
如果(此->列表大小==0){
此->开始=此->结束=新节点;
此->列表大小+=1;
}否则{
newNode->setPrev(结束);
此->结束=新节点;
此->列表大小+=1;
}
};
模板无效删除(节点){
Node*newNode=新节点;
*newNode=节点;
如果(此->列表大小==0){
此->开始=此->结束=空;
此->列表大小=0;
}否则{
如果(开始==结束)
此->结束=空;
this->end=newNode->getPrev();
newNode=end;
新建节点->设置下一步(空);
此->列表大小-=1;
}
};
模板void exibeList(){
节点*节点;

行业中的cout和业余爱好通用英语编码实际上是编写源代码的标准。您有什么好的理由打破它吗?模板应该在头文件中声明和定义,而不是像“常规”一样拆分为头文件/实现类。很可能这是你的问题。@Haroogan我在项目中总是用英语编码,但这是为我的类编写的,所以我用这种方式编码。@YOUSHI谢谢,我会尝试一下。你也有语法错误。
模板
格式不正确-如果你想要两个参数,每个参数都需要用
类型名
 模板
首先它被编码为
模板
,但我认为这可能是导致错误的原因,但事实并非如此。我正在尝试你的建议,但它似乎不再识别模板
// 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;
}