I have an error in the node class, when I try to do this with templates
the error that tells me: template arguments required for class Stack
Node class:
#ifndef NODO_H_INCLUDED
#define NODO_H_INCLUDED
#include <stdlib.h>
template <class T>
class Nodo{
private:
T Valor;
Nodo<T> *siguiente;
public:
Nodo(T v){
this->Valor=v;
this->siguiente=NULL;
}
void setSiguiente(Nodo<T> *sig){
this->siguiente=sig;
}
friend class Pila;
};
#endif // NODO_H_INCLUDED
Stack Class:
#ifndef PILA_H_INCLUDED
#define PILA_H_INCLUDED
#include "Nodo.h"
template <class T>
class Pila{
private:
Nodo<T> *Primero;
Nodo<T> *Ultimo;
bool PilaVacia(){
return (this->Primero==NULL);
}
public:
Pila(){
this->Primero=NULL;
this->Ultimo=NULL;
}
void Meter(T n){
Nodo<T> *nuevo=new Nodo<T>(n);
if(this->PilaVacia()){
this->Primero=nuevo;
}
else{
this->Ultimo->setSiguiente(nuevo);
}
this->Ultimo=nuevo;
}
};
#endif // PILA_H_INCLUDED
main.cpp
#include <iostream>
#include "Pila.h"
using namespace std;
int main()
{
Pila<int> pl;
pl.Meter(1);
cout << "Hello world!" << endl;
return 0;
}
[
At the moment it is the scheme that I must do, I don't know why it sends me to that error, I can't find a solution, I also don't know why it doesn't recognize the Stack class as a friend, if it already has the friend class Stack
Notice that in the file
Nodo
you are declaring the Stack class by:This brings you conflicts with the definition of
Pila
since one is a template and the other is not. You should simply use the class name:But since
Pila
it's a template you can't make all ofPila
the specs class-friendly. Just one at a time which is all you need:Also in the class
Nodo
there is none#include
of the classPila
. So to avoid cyclic inclusion problems better remove that line and write this one in the class filePila
:You would have something like this:
node.h
Stack.h
By the way, for now you don't need the classes to be friends since you're not accessing any private variables or functions of the other.
The code would look like this:
node.h
Stack.h
You can see other errors that may be clearer in another compiler and try it here .