basic stack functions

C

chirag

i wrote the following code for the comments given. however, i am getting
some errors in it. it says local function definitation are illegal.. plese
scan through the following code. thanks.

void Stack::print()
// Prints the contents of a stack from top to bottom. The stack
// is not changed. Does not call any Stack member functions.
{
int item;
if (aList.isEmpty())
throw StackException("Cannot print, stack is empty.");
cout << "The contents of a stack are : ";
for (int i = 1; i <= aList.getLength(); i++)
{
try
{
aList.retrieve(i, item);
}
catch (ListIndexOutOfRangeException e)
{
throw StackException("Retrieve index out of range");
}

cout <<item << " ";
}
}
 
A

Andre Kostur

i wrote the following code for the comments given. however, i am
getting some errors in it. it says local function definitation are
illegal.. plese scan through the following code. thanks.

void Stack::print()
// Prints the contents of a stack from top to bottom. The stack
// is not changed. Does not call any Stack member functions.
{
int item;
if (aList.isEmpty())
throw StackException("Cannot print, stack is empty.");
cout << "The contents of a stack are : ";
for (int i = 1; i <= aList.getLength(); i++)
{
try
{
aList.retrieve(i, item);
}
catch (ListIndexOutOfRangeException e)
{
throw StackException("Retrieve index out of range");
}

cout <<item << " ";
}
}

Your problem is before this code. Somewhere above you've got an opening
brace '{' and are missing the closing brace '}'.
 
M

Malte Starostik

chirag said:
i wrote the following code for the comments given. however, i am getting
some errors in it. it says local function definitation are illegal.. plese
scan through the following code. thanks.

void Stack::print()
// Prints the contents of a stack from top to bottom. The stack
// is not changed. Does not call any Stack member functions.
{
int item;
if (aList.isEmpty())
throw StackException("Cannot print, stack is empty.");
cout << "The contents of a stack are : ";
for (int i = 1; i <= aList.getLength(); i++)
{
try
{
aList.retrieve(i, item);
}
catch (ListIndexOutOfRangeException e)
{
throw StackException("Retrieve index out of range");
}

cout <<item << " ";
}
}

Any chance you're using an outdated compiler that doesn't know about
exceptions and parses the throw and catch statements as function
declarations resp. definition?
BTW, it's most probably better to catch that exception by const
reference instead of by value.

Cheers,
Malte
 
D

Donovan Rebbechi

Any chance you're using an outdated compiler that doesn't know about
exceptions and parses the throw and catch statements as function
declarations resp. definition?
BTW, it's most probably better to catch that exception by const
reference instead of by value.

While we're pointing out problems with the code:
(1) catch exceptions by reference
(2) if the stack really isn't changed, the function should be declared const.
(3) why force output to go to cout ? why not take a parameter of type
ostream&, then return the argument ? Then the stream insertion operator is
a one liner.
(4) why throw an exception when the stack is empty ? It's possible to print
an empty stack, so there's no need for the operation to fail.
(5) in fact the function should never throw an exception (well, maybe if
ostream insertion fails). Since the for loop checks bounds, an out of
range exception should never occur. Conditions that cause an exception
should be documented. If the point of the exception is to program
defensively, use a separate StackAssertion exception class instead,
to make the intention more clear.
(6) If aList is a linked list, the traversal is unnecessarily inefficient.
(7) why use "1 based counting" when the rest of the C++ world counts from 0 ?

Cheers,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,202
Messages
2,571,057
Members
47,661
Latest member
FloridaHan

Latest Threads

Top