0

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;

NiteRacer
  • 31
  • 4
  • 1
    `main` includes `Nodo.h`. `Nodo.h` immediately includes `Pila.h`. `Pila.h` immediately includes `Nodo.h`, but your include guards stop it, and it gets skipped. By the time you get to your first use of `Nodo`, `Nodo` hasn't been declared. If you want to see what I mean, find out how to make your compiler produce the Preprocessor Output. For gcc this is the `-E` flag, for example. – JohnFilleau Nov 28 '20 at 17:13
  • Right now, as written, your `Pila` doesn't need to be included in `Nodo`. `Nodo` doesn't need to know about `Pila`. Generally, your contained items don't need to know about the containers that contain them. Ask yourself, does an `apple` need to know about the existence of a `sack` in order to be an apple? Of course not. Your `Nodo` doesn't need to know about a `Pila` to do all the things necessary of a `Nodo`. – JohnFilleau Nov 28 '20 at 17:17
  • https://stackoverflow.com/questions/3742822/preprocessor-output – JohnFilleau Nov 28 '20 at 17:18

1 Answers1

0

You have both "Pila.h" and "Nodo.h" used. To use both correctly, you will need to have a forward declaration.

You could have declarations as follows:

class Nodo;
class Pila;

This tells the compiler that both "Nodo" and "Pila" are classes. The compiler doesn't need to know the structure of these two classes. Just the fact that they exist is sufficient.

Logicrat
  • 4,438
  • 16
  • 22