This is a fairly straight-forward problem, and it seems like it should be simple, but I'm not sure how to implement it. I'm probably getting stuck on syntax. If someone could just give me a HINT, I'd appreciate it.
#if (!defined(STACK_H))
#define STACK_H
#include "node.h"
class Stack {
Node head;
public:
// Push new element to the stack:
void push( int value );
// Pop element from the stack:
int pop();
// Writeable access to element on top of the stack:
int& top();
// Read-only access to element on top of the stack:
int top() const;
// Return stack size:
size_t size() const;
// Is stack empty?
bool is_empty() const;
// reverse the stack
void reverse();
};
#endif
I did some test output. The overloaded top() isn't called in main, but when I made a call with my previous definition, it didn't output what I wanted it to. I should output the same as the other version. The only difference is the return type. I'm trying to figure out what to write for the definition to make this happen.
You can't overload a function by changing only its return value.
What do you want to achieve by that? Shouldn't the overload return a int & too? If you return an int, you are not granting read-only access to the top, you are returning a copy of the value (which is the same for ints, but surely different for non-trivial objects).
Ah, I thought that was also considered overloading. Regardless, that's the way the program needs to be written. Returning a copy of the value in this case isn't "access" but it is still read-only.