for every method in my cpp file for a simple stack postfix calculator, the compiler returns the error that the stack I am using is 'not declared in this scope'. could someone please help me? the code is below.
well, that certainly stopped the errors from doubling up, but it doesn’t solve the problem entirely.
here is the error log,since its no longer enormous.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
In file included from postfixCalculator.cpp:4:0:
postfixCalculator.h:13:17: error: expected ‘;’ at end of member declaration
postfixCalculator.cpp:6:19: error: expected unqualified-id before ‘)’ token
postfixCalculator.cpp: In function ‘void add()’:
postfixCalculator.cpp:10:8: error: ‘s’ was not declared in this scope
postfixCalculator.cpp: In function ‘void subtract()’:
postfixCalculator.cpp:20:8: error: ‘s’ was not declared in this scope
postfixCalculator.cpp: In function ‘void multiply()’:
postfixCalculator.cpp:30:8: error: ‘s’ was not declared in this scope
postfixCalculator.cpp: In function ‘void divide()’:
postfixCalculator.cpp:40:8: error: ‘s’ was not declared in this scope
postfixCalculator.cpp: In function ‘void addNum(int)’:
postfixCalculator.cpp:50:5: error: ‘s’ was not declared in this scope
postfixCalculator.cpp: In function ‘int getTopNum()’:
postfixCalculator.cpp:53:12: error: ‘s’ was not declared in this scope
In file included from testPostfixCalc.cpp:2:0:
postfixCalculator.h:13:17: error: expected ‘;’ at end of member declaration
All your functions are just functions in the global scope, they are not member functions. You need to scope them to let the compiler know that they are defining member functions, like in this example:
1 2 3 4 5 6 7
struct SomeClass
{
SomeClass();
~SomeClass();
int SomeMemberFunction(int x, int y);
};
okay, that helped a lot. down to just three things in the error log....
1 2 3 4 5
In file included from postfixCalculator.cpp:4:0:
postfixCalculator.h:13:17: error: expected ‘;’ at end of member declaration
postfixCalculator.cpp:6:19: error: expected unqualified-id before ‘)’ token
In file included from testPostfixCalc.cpp:2:0:
postfixCalculator.h:13:17: error: expected ‘;’ at end of member declaration
]
unfortunately, I have no idea what these things mean. Does this also have to do with scope?
You forgot a semi-colon after the declaration of getTopNum().
Also, stack isn't dynamic, so you don't need to call s = new stack() (and you're not allowed to) in your constructor.
Your missing ; after your member functions. Compiler errors can be a pain to understand and reconize when your just starting. One thing that helped me was to program something that I knew worked withouterrors like the hello world program then start removing things or changing them to see what kind of errors I got. Since you k ow what you changed you know what the error message means so eventually you start to understand them better. Hope that helps and sorry for the typos on my phone