Click here to Skip to main content
15,881,588 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I was reading some text about the implementation of Stack using using our own class. The definition for this is placed in Stack.h file and code in Stack.cpp file. Here's my Stack.h file
typedef char Stack_entry;
const int maxstack=10;
class Stack
{
public:
Stack();
Error_code push(const Stack_entry &item);
Error_code pop();
Error_code top(Stack_entry &item)const;
bool empty()const;
private:
int count;
Stack_entry entry[maxstack];
};


In one of my methods in Stack.cpp, I write
Error_code Stack::push(const Stack_entry &item)
/*Post:Count is increased by one if the outcome is success*/
{
Error_code outcome=sucess;
if(count>=maxstack)
{
outcome=overflow;
}
else
{
entry[count++]= item;
}
return outcome;
}

I am confused if this Error_code was being used in general context by the author?
If not, how do we mention the type for Error_code?
Error_code has a variable named outcome which is initially set to success. When this is returned actually, what's being returned?
For eg: When we return, we know we are returning a number, char returns an alphabet but what is this type Error_code?

I have declared three methods as Error_code in Stack.h as
Error_code push(const Stack_entry &item);
Error_code pop();
Error_code top(Stack_entry &item)const;

While compiling my own Stack class, I am getting the error that Error_code does not name a type. :-|
Posted
Updated 23-Dec-10 6:06am
v2

The Error_code is probably an enum, like the following:
C++
enum Error_code
{
  success,
  // other error code items here
  overflow
};


I suppose you should check again the book, looking for its actual definition.
:)
 
Share this answer
 
Comments
optimus_prime1 24-Dec-10 1:56am    
Where is this Error_code definition mentioned? In the global part of the Stack.h definition file?
I tried mentioning this enum in the global part of Stack.h but Stack.h says 'success' was not declared in the scope.

Also, in my Stack.h file, I have mentioned at the top typedef char Stack_entry, but that makes the Stack only to take char values. If I do not declare this in Stack.h file but do this in main.cpp by the client code, then I get error saying Stack_entry does not name a type.
CPallini 24-Dec-10 2:41am    
Did you check the Book for the actual definition? In any case, copying the code snipped I posted at the top of your Stack.h header file should do the trick.
optimus_prime1 24-Dec-10 4:23am    
Yes, I did mention the enum code at the top of my Stack.h class definition like this:

const int maxstack=10;
enum Error_code { success, fail, range_error, underflow, overflow, fatal,
not_present, duplicate_error, entry_inserted, entry_found,
internal_error };
class Stack
{
public:
Stack();
bool empty()const;
Error_code pop();
Error_code top(Stack_entry &item)const;
Error_code push(const Stack_entry &item);
private:
int count;
Stack_entry entry[maxstack];
};

but in Stack.cpp, I am still getting errors "success was not declared in this scope."

My second doubt: I have to make this Stack_entry generic by allowing the user to mention define in his code like typedef char Stack_entry; but if I remove this from Stack.h, errors are: "Stack_entry does not name a type"
CPallini 24-Dec-10 6:16am    
(1) Nope: you are getting "sucess" (with a single 'c') "was not declared in this scope" (you made a typo).
(2) To allow for generic datatypes you should build a template class instead.
Right click on it and see if you see "go to definition" on your compiler. If not, search for enum Error_code, #define Error_code, struct Error_code, or class Error_code to track it down.

Hopefully it is NOT an enum since it will most likely be poorly designed if it is.
 
Share this answer
 
v2
Comments
optimus_prime1 24-Dec-10 1:17am    
Yes, it was an enum and it was a text example from the Data Structures book I was referring to learn all this.
CPallini 24-Dec-10 2:42am    
@Ted: Why using enum is 'poor design'?
T2102 24-Dec-10 8:24am    
Using enum for error codes is one of the first textbook examples of poor design in a graduate text I read on developing large scale systems. There are many problems with using an enum for an error code, including that adding additional error codes forces recompiles. At some point, people avoid adding them and just chose a generic error code that is uninformative.
T2102 24-Dec-10 8:31am    
Unless you're dealing with a very small system, you are better off creating a pattern where you register error codes or create them dynamically.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900