Can't call function inside another class.

I've been stuck on this for days. My teacher gave us the *.h to work with and we have to build the implementations. I'm trying to get this class to call a function from another class, but I always get an error. It says it cannot call the member function (stack::push(request)) without an object.

Please help...

calc.h
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
 13 #ifndef CALCULATOR                                                                                                                 
 14 #define CALCULATOR                                                                                                                 
 15                                                                                                                                    
 16 // Calculation struct to store type of calculation and value.                                                                      
 17 // This gets put together by Calculator class and passed to                                                                        
 18 // the stack, queue, and client application.                                                                                       
 19 struct calculation                                                                                                                 
 20 {                                                                                                                                  
 21   char type;
 22   double value;
 23 };                                                                                                                                 
 24 
 25 // Linear and circular linked list node.                                                                                           
 26 struct node
 27 {
 28   calculation * data; // Pointer to calculation struct.                                                                            
 29   node * next; 
 30 };
 31   
 32 
 33 // Calculator - Uses stack and queue to store calculations.                                                                        
 34 // The calculator creates a running total based on the commands                                                                    
 35 // requested once the calculation request takes place.                                                                             
 36 class calculator                                                                                                                   
 37 {
 38 public:
 39   calculator();                                                                                                                    
 40   ~calculator();
 41   int add(double value);
 42   int subtract(double value);                                                                                                      
 43   int multiply(double value);
 44   int divide(double value);
 45   int calculate(); // Perform all requested calculations.                                                                          
 46   int last_operation(calculation & request); // Tell client what last calculation was.                                             
 47   int undo(int number); // Performs undo request a number of times.                                                                
 48   int undo(calculation & request); // Undos until a particular calculation request.                                                
 49   int view_history(); // Displays the queue, not stack.                                                                            
 50 
 51 private:
 52   node * head; // Store calculations to be performed by precendence.                                                               
 53 };
 54 
 55 #endif 


stack.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
 15 class stack
 16 {
 17 public:
 18   stack(); // Default constructor.
 19 //  stack(int size); // Receive size of calculation and dynamically allocate it.
 20   ~stack();
 21   int push(const calculation & request);
 22   int pop();
 23   int peek(calculation & request) const; // Tells the client what the last calculation was.
 24   int display() const;
 25 
 26 private:
 27   node * head;
 28 };


calc.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
 13 #include "calc.h"
 14 #include "stack.h"
 15 
 16 // Constructor
 17 calculator::calculator()
 18 {
 19 }
 20 
 21 // Deconstructor
 22 calculator::~calculator()
 23 {
 24 }
 25 
 26 // Add numbers.
 27 int calculator::add(double value)
 28 {
 29   int result = 0;
 30   calculation request;
 31   request.type = '+';
 32   request.value = value;
 33   result = stack::push(request);
 34   return 1;
 35 }
closed account (SECMoG1T)
Yea you can't call a member function that way you must create an instance of the class the call the function on that object, think of it this way even if it was legal to push your requests by calling your stack::push(request); how would you pop those requests

What you need is a local stack, use to store your request , i would suggest that you add a stack member to your class calculator then you can use that member to store those requests
Last edited on
In class calculator, I think
node * head;
is probably supposed to be
stack stk;
Ask your professor about it. If you want to be snarky you might also ask:
- why is class node in calc.h when it should be in stack.h?
- what should the return values of all these methods be? If he/she responds that they return success/failure, point out that returning bool would be more appropriate than int.
- You see that class node points to a calculation. Is that supposed to be allocated on the heap? If so then who should delete it? If class node then shouldn't it have a destructor? Wouldn't it make more sense for class node to have an instance of class calculation rather than a pointer to one?

Sorry guys. I forgot to mention it, but my teacher made the member functions in the *.h files. She didn't make the data members or any constructor/destructor. I'll try it out here in a little bit, but what you guys say make total sense. I really appreciate the help.

dhayden: That one is a struct node and struct calculation. I think I made the names a little confusing. There's a class calculator and struct calculation. :P Should it still be an instance or is pointer ok? Also, she told us she prefers to use an int return type even though it's a simple success/failure return type. She said an int does the same thing, but gives you extra information if you should ever need it.
Am I supposed to put stack request; in the private section of class calculator? It's giving me an error and saying that 'stack' does not name a type.

The way my professor wants it, we're building as if someone will use the calculator we built with the functions in the class calculator. They should not know anything about a stack, queue, or any concept of nodes and abstract data types. We have to use the member functions provided. We can add more, but they can't know anything about the data structure.


UPDATE: Alright, I got it to work. I had to put class stack request; to use it. It compiled. Thanks again guys.
Last edited on
Topic archived. No new replies allowed.