0

私のデータ構造の先生は、リストとスタックを手動で実装するタスクを私たちに与え、それはうまく機能しましたが、テンプレートを使用して任意のタイプのパラメーターを受け入れるように再実装する必要がありました. それ以来、私はさまざまなエラーに直面してきました。現在、c2019 エラーが発生していますが、コードの何が問題なのかわかりません。this: pilha.getTopo<int>();、 this: Lista<int> lista;、 this: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;
}

前もって感謝します。

4

0 に答える 0