Ir para conteúdo

POWERED BY:

Arquivado

Este tópico foi arquivado e está fechado para novas respostas.

Henrique Barcelos

Erro 'is not a type' + templates

Recommended Posts

Boa tarde galera...

 

Estou com um problema aqui...

 

Tenho um trabalho de Estruturas de Dados pra entregar segunda feira, tava indo tudo bem até aparecer um tal erro 'is not a type'...

 

Estou implementando uma Pilha usando Listas Encadeadas, com parametrização através de templates.

 

Seguem os source codes:

 

main.cpp

#include <cstdlib>
#include <iostream>
#include "PilhaEncadeada.h"

using namespace std;

int main(int argc, char** argv) {
	Stack<int> pilha = new Stack();

	return (EXIT_SUCCESS);
}

PilhaEncadeada.h

#ifndef _PILHAENCADEADA_
#define	_PILHAENCADEADA_

#include "Node.h"

using namespace std;

template<class T>
class Stack {
private:
	Node<T> *first;
public:
	Stack():first(NULL){}
	~Stack(){}
	bool push(T el){
 	Node<T> *n = new Node();

 	n->setInfo(el);
 	n->setNext(first);

 	first = n;
	}
};

#endif	/* _PILHAENCADEADA_ */

 

Node.h

#ifndef _NODE_H
#define	_NODE_H

#include <cstdlib>
#include <iostream>

using namespace std;

template <class T>
class Node {
private:
	Node *next;
	T info;
public:
	Node(){
 	next = NULL;
	}

	Node(T el, Node *n){
 	setInfo(el);
 	setNext(n);
	}

	void setInfo(T el){
 	info = el;
	}

	T getInfo(){
 	return info;
	}

	void setNext(Node *n) {
 	next = n;
	}

	Node * getNext(){
 	return next;
	}

	void print(){
 	cout << "Valor: " << info << endl;
 	cout << "Ponteiro: " << hex << next << endl;
	}

	Node & operator = (const Node & right){
 	if(this != &right){
 	setInfo(right.info);
 	setNext(right.next);
 	}
 	return (*this);
	}
};

#endif	/* _NODE_H */

 

Agradeço qualquer luz...

 

Ah eh...

 

Aparece no console:

 

PilhaEncadeada.h:24: error: `Node' is not a type
main.cpp:15: error: `Stack' is not a type

Compilador: MinGW

IDE: NetBeans

Compartilhar este post


Link para o post
Compartilhar em outros sites

Na definição da classe Node, você declara instâncias da classe Node...

Mas Node é um template, portanto é necessário especificar um tipo.

 

ERRADO:

template<class T>
class Node
{
    private:
        Node *next;

    //... etc ...
};

CERTO:

template<class T>
class Node
{
    private:
        Node<T> *next;

    //... etc ...
};

Compartilhar este post


Link para o post
Compartilhar em outros sites

×

Informação importante

Ao usar o fórum, você concorda com nossos Termos e condições.