473,320 Members | 2,088 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

basic stack functions

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 << " ";
}
}

Jul 23 '05 #1
4 4142
chirag schrieb:
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
Jul 23 '05 #2
"chirag" <ch*******@yahoo.com> wrote in
news:e8******************************@localhost.ta lkaboutprogramming.com:
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 '}'.
Jul 23 '05 #3
it is ms visual studio that i m using

Jul 23 '05 #4
On 2005-03-17, Malte Starostik <ma***@starostik.de> wrote:
chirag schrieb:
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.


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,
--
Donovan Rebbechi
http://pegasus.rutgers.edu/~elflord/
Jul 23 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
by: Leif K-Brooks | last post by:
Has anyone ever tried implementing a simple unstructured BASIC dialect in Python? I'm getting interested in language implementation, and looking at a reasonably simple example like that could be...
4
by: Chris Mabee | last post by:
Hello all, and Merry Christmas, I'm having a problem understanding an example of an array based implementation of a stack in a textbook of mine. The code in question is written below. The syntax...
3
by: Thomas Ibbotson | last post by:
I've been going through a tutorial on C++ and I've come across what I regard as some strange behaviour. As part of the tutorial on classes a simple stack is implemented with functions push() and...
13
by: Kenneth Lantrip | last post by:
In Ansi-C C99 or Standard C or in GCC... Is there a way to push a value onto the stack and then later retrieve it within the same function? void foo(void) {
14
by: luis | last post by:
Are basic types (int, long, ...) objetcs or not? I read that in C# all are objects including basic types, derived from Object class. Then in msdn documentation says that boxing converts basic...
3
by: Jeremy | last post by:
While working with ASP.NET I've sometimes encountered errors in my applications, but I've always known exactly why I'm getting the error and have been able to fix it. So I've never needed to...
9
by: Ajay | last post by:
Hi all, Can I know what is the stack space and heap space allocated by the compiler.Can i increase it or decrease it.if yes,pleae tell me theway to do it.Thanks in advance. Cheers, Ajay
4
by: voidtwerp | last post by:
Hi, I hope this is not too OT but I would like clarification on how classes are held in memory. each object obviously has an individual copy of its data. I guess a class would only have one...
16
by: sarathy | last post by:
Hi all, I need a few clarifications regarding memory allocaion in C++. I apologize for the lengthy explanation. 1. In C++, Objects are allocated in heap. What does heap refer to? Is it an area...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.