0

my data structure teacher gave us the task to implement lists and stacks manually and it worked just fine, but then we had to reimplement it to accept any type of parameters using template. Since then, I've been facing lots of different errors. Right now, I'm having a c2019 error, but I can't see what's wrong with my code. Tried this:pilha.getTopo<int>();, this: Lista<int> lista;, this: typedef Lista<int> Lista_int;, but no luck.

Now, my code:

######UPDATE: The code bellow is working

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);
        }
    };
};

and finally, 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;
}

Thanks in advance.

ildjarn
  • 62,044
  • 9
  • 127
  • 211
  • 2
    In the industry and hobby coding in general English is de facto standard for writing the source code. Do you have any good reason to break it? – Alexander Shukaev Apr 15 '13 at 00:59
  • Templates should be both declared and defined within a header file, not split into header/implementation like a "regular" class. Likely this is your problem. – Yuushi Apr 15 '13 at 01:08
  • @ Haroogan I always code in english on my projects, but this is for my class, so I coded it this way. @Yuushi Thanks, I'll try it. – Gustavo Coelho Apr 15 '13 at 01:14
  • You also have syntax errors. `template ` isn't well formed - if you want 2 parameters, they each need to be specified with `class` or `typename`: `template ` – Yuushi Apr 15 '13 at 01:18
  • First it was coded as `template ` but I thought could be this what was causing the errors, but wasn't. I'm trying your suggestion here, but it seems to do not recognize the template any longer – Gustavo Coelho Apr 15 '13 at 01:22
  • @Yuushi Not necessarily. You can separate templates into interface/implementation if you use [explicit instantiation](http://stackoverflow.com/a/13069350/1227469). – JBentley Apr 15 '13 at 01:44
  • @JBentley Very uncommon, and definitely not applicable for someone just starting to learn how to use templates. – Yuushi Apr 15 '13 at 02:07

0 Answers0