0

I have a problem with next code, i get the following error

Stack.h:13:3: error: ‘Cell’ does not name a type

I don't understand how to solve that, thanks for your help.

// in a stack, we are in interested in its top value only. The default constructor initializes the stack to
// be empty
class Stack {
    public:
        Stack();
        void push(int value); // push adds a value at the top of the stack 
        int top(); // returns the top value
        void pop(); // removes the top value
        int size() const;  // size returns the number of values in the stack 
        bool empty() const; // returns true if the stack is empty
        int getNext() const;
    private:
        Cell *m_firstCellPtr; // m_firstCellPtr field is a pointer to the first cell of the linked list holding the values of the stack 
        int m_size; // fields holds the current size of the stack

};

Every example i have got of the book C++17 By Example By Stefan Björnander February 2018

William Torrez
  • 1
  • 1
  • 1
  • 6
  • 2
    Did you include the header for `Cell`? If you did make sure the header for `Cell` does not include the header for `Stack` – drescherjm Jan 15 '19 at 22:02
  • Lots of possible reasons for this. Could we have a [mcve]? – user4581301 Jan 15 '19 at 22:07
  • Would i include the header _Cell.h_ for _Stack_? – William Torrez Jan 15 '19 at 22:12
  • 1
    You could do that, but in general it's best to forward-declare types that are used as pointers or references in another header. _i.e._ just declare `class Cell;` somewhere before your `Stack` definition. In `Stack.cpp` you would still include `Cell.h` – paddy Jan 15 '19 at 22:14
  • I include _Cell.h_ in _Stack.cpp_, but i have the same error. Too declare _class Cell_ in another header. – William Torrez Jan 15 '19 at 22:23
  • The first three lines of code in that book doesn't look too promising. `using namespace std; void main() { srand( ...` – Ted Lyngmo Jan 15 '19 at 22:25
  • ***that book doesn't look too promising*** It currently has a 2 out of 5 rating on Amazon but only 5 total reviews. – drescherjm Jan 15 '19 at 22:29
  • I get too error with this book. Only the first examples to compile. – William Torrez Jan 15 '19 at 22:35
  • 1
    `I include Cell.h in Stack.cpp` but your error is in Stack.h, include Cell.h in Stack.h. – john Jan 15 '19 at 22:40
  • Compilers are very stupid. The compiler is reading Stack.h from begining to end and if at the point you starting talking about `Cell` the compiler hasn't seen a declaration of `Cell` its going to complain. It doesn't look around for a definition somewhere else, you have to include the definition yourself, in this case by #including Cell.h in Stack.h. – john Jan 15 '19 at 22:44
  • You could also do #2 in the current answer and leave the `#include "Cell.h"` alone in `Stack.cpp` – drescherjm Jan 15 '19 at 22:52
  • @john include _Cell.h_ in _Stack.h_. I get this: error: redefinition of ‘class Cell’ – William Torrez Jan 15 '19 at 22:55
  • @MuxMux Well need to see some real code then, can't debug this without it. – john Jan 15 '19 at 22:56
  • 1
    @MuxMux Probably your header files are missing *include guards*, do you know about those? I notice Stack.h above doesn't have any include guards, but maybe you didn't bother to post that part of the header file. – john Jan 15 '19 at 22:57
  • @john hi John. https://github.com/Villelmo/Stack – William Torrez Jan 16 '19 at 20:22
  • @MuxMux Yes as I thought, no include guards. You should write headers on the assumption that they may be included several times in a single compilation. Include guards are the answer to this issue. They're very easy, here's a slightly long winded explanation. https://stackoverflow.com/questions/8020113/c-include-guards – john Jan 16 '19 at 20:31

1 Answers1

3

Line Cell *m_firstCellPtr; refers to a type Cell which is obviously not known at that point.

This is very likely because the book just shows the Stack-portions on this page without mentioning that you will have to declare Cell, e.g. by including something like a #include "Cell.h"

Three ways to get out of this:

  1. #include "Cell.h", if the book's ressources provide it
  2. forward declare Cell as class Cell; before the definition of Stack;
  3. change the line to class Cell *m_firstCellPtr;
Stephan Lechner
  • 34,891
  • 4
  • 35
  • 58