issues with scope, among other things

Feb 5, 2013 at 5:59pm
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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include <iostream>
#include <stack>
#include <cmath>
#include "postfixCalculator.h"
using namespace std;
postfixCalculator() {
  s=new stack();
}
void add(){
  if(!(s.empty())){
    int a=s.top();
    s.pop();
    int b=s.top();
    s.pop();
    int c=a+b;
    s.push(c);
  }
}
void subtract(){
  if(!(s.empty())){
    int a=s.top();
    s.pop();
    int b=s.top();
    s.pop();
    int c=a-b;
    s.push(c);
  }
}
void multiply(){
  if(!(s.empty())){
    int a=s.top();
    s.pop();
    int b=s.top();
    s.pop();
    int c=a*b;
    s.push(c);
  }
}
void divide(){
  if(!(s.empty())){
    int a=s.top();
    s.pop();
    int b=s.top();
    s.pop();
    int c=a/b;
    s.push(c);
  }
}
void addNum(int x){
    s.push(x);
  }
int getTopNum(){
    return s.top();
  }
header file//////////////////////////////////////////////////////////////////
#include <iostream>
#include <stack>
using namespace std;

class postfixCalculator {
 public:
  postfixCalculator();
  void add();
  void subtract();
  void multiply();
  void divide();
  void addNum(int);
  int getTopNum()
  private:
 stack <int> s;
};


test method///////////////////////////////////////////////////////////////////
#include <iostream>
#include "postfixCalculator.cpp"
#include <cmath>
using namespace std;
int main() {
  postfixCalculator p;
	p.addNum (1);
	p.addNum (2);
	p.addNum (3);
	p.addNum (4);
	p.addNum (5);
	p.add();
	p.add();
	p.add();
	p.add();
	cout << "Result is: " << p.getTopNum() << endl;
	return 0;
}

any help would be appreciated.
Last edited on Feb 5, 2013 at 7:32pm
Feb 5, 2013 at 6:16pm
please edit your question using code tags (hint: it is the button that looks like <>)
Feb 5, 2013 at 6:20pm
Yeah. No offense to the OP, it being your first post and all. But I've given up reading code without the tags. (:
Feb 5, 2013 at 7:15pm
Maybe I missed something, I can't see the contents of the header file "postfixCalculator.h"
Feb 5, 2013 at 7:34pm
noob first post mistakes fixed.
Feb 5, 2013 at 7:43pm
Alrighty, thank you :D

Try including "postfixCalculator.h" instead of "postfixCalculator.cpp"
Feb 5, 2013 at 7:49pm
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
Feb 5, 2013 at 7:54pm
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);
};
1
2
3
4
5
6
7
8
9
10
11
SomeClass::SomeClass()
{
}
SomeClass::~SomeClass()
{
}

int SomeClass::SomeMemberFunction(int x, int y)
{
    return x+y;
}


This is also described below by BlackSheep:
Last edited on Feb 5, 2013 at 7:56pm
Feb 5, 2013 at 7:55pm
You need to let the compiler know that the functions add, subtract etc. belong to the class postfixCalculator:
1
2
3
4
void postfixCalculator::add() //Prefix postfixCalculator:: to the function definitions
{
//Code
}
Feb 5, 2013 at 8:03pm
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?
Feb 5, 2013 at 8:06pm
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.
Feb 5, 2013 at 8:08pm
closed account (3qX21hU5)
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
Last edited on Feb 5, 2013 at 8:12pm
Feb 5, 2013 at 8:10pm
are you referring to the header file when you say declaration?
Feb 5, 2013 at 8:16pm
Yupperdoodle.
Declaration is when you "declare" a function without providing a body:
 
int function(int a);

Definition is when you actually define the function's body.
Last edited on Feb 5, 2013 at 8:17pm
Feb 5, 2013 at 8:18pm
and, error free! thanks, internet!
Topic archived. No new replies allowed.