I'm trying to learn C++ and currently I am implementing a stack using pointers, but I have a problem using headers and the include directive.
Here's my code:
Main.cpp:
#include <stdio.h>
#include <iostream>
#include "Nodo.h"
#include "Pila.h"
using namespace std;
int main() {
Pila pila;
cout << "Apilar(20)" << endl;
pila.apilar(10);
pila.apilar(20);
pila.apilar(30);
cout << pila.desapilar() << endl;
cout << pila.desapilar() << endl;
cout << pila.desapilar() << endl;
return 0;
};
Pila.h:
#ifndef PILA_H
#define PILA_H
#include "Nodo.h"
#include <cstddef>
class Pila {
private:
Nodo cima;
public:
Pila();
~Pila();
void apilar(int v);
int desapilar();
};
#endif // PILA_H
Pila.cpp:
#include "Pila.h"
#include "Nodo.h"
Pila::Pila(){
cima = NULL;
}
Pila::~Pila()
{
while(cima) desapilar();
}
void Pila::apilar(int v)
{
Nodo nuevo;
nuevo = new Nodo(v, cima);
cima = nuevo;
}
int Pila::desapilar()
{
Nodo nodo;
int v;
if (!cima) return 0;
nodo = cima;
cima = nodo->siguiente;
v = nodo->valor;
delete nodo;
return v;
}
Nodo.h:
#ifndef NODO_H
#define NODO_H
#include "Pila.h"
class Nodo {
private:
int valor;
Nodo *siguiente;
friend class Pila;
public:
Nodo(int v, Nodo *sig);
};
#endif // NODO_H
Nodo.cpp:
#include "Nodo.h"
Nodo::Nodo(int v, Nodo *sig){
valor = v;
siguiente = sig;
};
The output of my program is the following: error: 'Nodo' does not name a type, I think it has to do with the include directive but I dont know what to do to solve it. Thanks in advance.
EDIT: my bad, didn't indicate in what line of code the error was. It is in the 8 line of the Pila.h file, in Nodo cima;