1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

strange error in stack program : try and catch statement

Discussion in 'C++' started by namrata, Sep 29, 2008.

  1. namrata

    namrata New Member

    Joined:
    Jan 28, 2007
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    Hi
    I am getting very strange error in stack program when I am compiling it
    Stack.cpp:45: error: expected unqualified-id before "try"
    Stack.cpp:51: error: expected unqualified-id before "catch"
    Stack.cpp:58: error: expected unqualified-id before "catch"

    can anyone resolve it here's my code
    Code:
    StackType::StackType()
    
    {
    top = -1;
    }
    
    
    bool StackType::IsEmpty() const
    {
    return (top == -1);
    }
    
    bool StackType::IsFull() const
    {
    return (top == MAX_SIZE-1);
    }
    
    
    void StackType:ush(char newItem)
    {
    if (IsFull())
    throw FullStack();
    top++;
    items[top] = newItem;
    }
    
    try
    {
    
    stack.Push(item) ;
    cout<<item<<"pushed onto stack"<<endl;
    
    }catch (FullStack exceptionObject)
    
    {
    cerr << "FullStack exception thrown" << endl;[/b]}
    
    catch (EmptyStack exceptionObject)
    {
    cerr<< "EmptyStack exception thrown" << endl;
    << "Exiting with error code 2" << endl;
    exit(2);
    }
    
    char StackType:op()
    {
    if(IsEmpty())
    throw EmptyStack();
    top--;
    }
    
    char StackType::Top()
    
    {
    if (IsEmpty())
    throw EmptyStack();
    return items[top];
    }
     
    Last edited by a moderator: Sep 29, 2008
  2. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    Your try...catch block should be in a function, but it isn't. Is it meant to be within the StackType:ush function?

    Are the functions meant to be called ush and op, r id ou ean o iss ff he irst etter?
     
  3. ahamed101

    ahamed101 New Member

    Joined:
    Sep 27, 2008
    Messages:
    10
    Likes Received:
    0
    Trophy Points:
    0
    Yes, your try...catch block cannot be a stand alone stuff... It should be within a function...
     

Share This Page