My struct type is not recognized within my class

0

People, I'm doing the implementation of a stack with pointers in C ++ and I have the following code for now:

template <class T>
struct Node {
    T item;
    Node *prox;
};

class Pilha {
    private:
    int tamanho;
    Node *topo;

    public:
    Pilha() {
    this->topo=NULL;
    this->tamanho=0;    
}

In the top private attribute, the Node type is not being recognized within my Stack class. The eclipse returns the following error: Type 'Node' could not be resolved .

What can it be? Thank you!

    
asked by anonymous 24.09.2018 / 01:39

1 answer

1
The problem is that its Node structure has a template , which will be used to create a Node of any type, so you must also use this pattern in the Pilha class for the compiler to know the type of Node may vary:

template <class T>
struct Node {
    T item;
    Node *prox;
};

template <class T> // indicação que esta classe tambem usa template
class Pilha {
private:
    int tamanho;
    Node<T> *topo; //aqui o Node é indicado com <T> 

public:
    Pilha() {
        this->topo=NULL;
        this->tamanho=0;
    }
};

In the code above, I only commented on the places I've changed.

Remember that when instantiating a node you must use the template notation. Example:

Node<T> *novoNo = new Node<T>();

In c ++ you also have a better alternative to NULL which is nullptr and that is a pointer literal and avoids some implicit conversions that in certain situations cause problems.

    
24.09.2018 / 12:11