help soon: declaration terminated incorrectly

This is a discussion on help soon: declaration terminated incorrectly within the C++ Programming forums, part of the General Programming Boards category; hi all, i am new here :) i have my pracs tmrw, and so was trying out a few programmes ...

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    8

    help soon: declaration terminated incorrectly

    hi all,

    i am new here :)

    i have my pracs tmrw, and so was trying out a few programmes to be prepared...

    but im stuck right now...

    i tried a lot to find my mistake, but didnt succeed...

    i hope u guys can help me - please, its urgent...

    here is the code:

    Code:
    //3 linked stack
    
    
    #include<conio.h>
    #include<iostream.h>
    #include<process.h>
    
    
    struct node
    {
    int a,b;
    node *next;
    };
    
    
    class linkedstack
    {
    node* top;
    public:
    linkedstack()
    {
    top==NULL;
    }
    ~linkedstack()
    {
    node *ptr;
    ptr=top;
    while(ptr!=NULL)
    {
    top=ptr->next;
    delete ptr;
    ptr=top;
    }
    void push();
    void pop();
    void display();
    };
    
    
    void linkedstack::push()
    {
    node* newnode;
    newnode = new node;
    cout<<"enter a";
    cin>>newnode->a;
    cout<<"enter b";
    cin>>newnode->b;
    newnode->next=NULL;
    if(top==NULL)
    top=newnode;
    else
    {
    top=newnode;
    newnode->next=top;
    }
    }
    
    
    void linkedstack::pop()
    {
    node *ptr;
    if(top==NULL)
    cout<<"underflow";
    else
    {
    ptr=top;
    top=top->next;
    delete ptr;
    }
    }
    
    
    void linkedstack::display()
    {
    node *ptr;
    if(top==NULL)
    cout<<"underflow";
    else
    {
    ptr=top;
    while(ptr!=NULL)
    {
    cout<<ptr->a;
    cout<<ptr->b;
    ptr=ptr->next;
    }
    }
    }
    
    
    
    
    void main()
    {
    int ch;
    linkedstack L1;
    
    
    
    
    do
    {
    clrscr();
    cout<<"1. Push element\n";
    cout<<"2. Pop element\n";
    cout<<"3. Display elements\n";
    cout<<"4. Exit\n";
    cout<<"Enter choice\n";
    cin>>ch;
    
    
    if(ch==1)
    {
    L1.push();
    getch();
    }
    
    
    else if(ch==2)
    {
    L1.pop();
    getch();
    }
    
    
    else if(ch==3)
    {
    L1.display();
    getch();
    }
    
    
    else
    exit(0);
    
    
    }
    while(ch!=4);
    }

  2. #2
    Registered User
    Join Date
    Dec 2012
    Posts
    13
    Hi,
    Few mistakes I found in your code were:
    1. You haven't closed your destructor correctly. You have just closed the while loop but not the destructor.
    2. While pushing, Shouldn't it be
    newnode->next=top;
    top=newnode;
    3. While poping don't you have to pass the node to the function that called pop.. Maybe by passing a node argument by reference..


  3. #3
    Registered User
    Join Date
    Jan 2013
    Posts
    8
    Quote Originally Posted by Sajas K K View Post
    Hi,
    Few mistakes I found in your code were:
    1. You haven't closed your destructor correctly. You have just closed the while loop but not the destructor.
    2. While pushing, Shouldn't it be
    newnode->next=top;
    top=newnode;
    3. While poping don't you have to pass the node to the function that called pop.. Maybe by passing a node argument by reference..

    thank you so much! :))
    i cant believe i didnt notice the destructor bracket *facepalm*
    as soon as i closed the bracket, it started working! :)

    another quick question: while declaring pointers, which one should i use - node* ptr or node *ptr?
    you can see, in my prog i am pretty confused about it, but it shows no error. so does that mean either way is okay?

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    2,917
    so does that mean either way is okay?
    Yes, either way is okay. But you should be consistent. Also using a consistent indentation style will make your programs much easier to read.

    Jim

  5. #5
    Registered User
    Join Date
    Jan 2013
    Posts
    8
    Quote Originally Posted by jimblumberg View Post
    Yes, either way is okay. But you should be consistent. Also using a consistent indentation style will make your programs much easier to read.

    Jim
    thank you so much!
    now getting back to my pracs...

    have a nice day you two! tc :)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 1 Error : Declaration Terminated Incorrectly
    By Inderjeet in forum C++ Programming
    Replies: 7
    Last Post: 07-01-2011, 02:25 AM
  2. bcc 5.5 Declaration terminated incorrectly
    By P4R4N01D in forum Windows Programming
    Replies: 9
    Last Post: 05-28-2008, 05:45 PM
  3. error declaration terminated incorrectly help
    By belfour in forum C++ Programming
    Replies: 7
    Last Post: 11-25-2002, 09:07 PM
  4. Declaration terminated incorrectly
    By Griffin2020 in forum C Programming
    Replies: 8
    Last Post: 04-27-2002, 12:53 AM
  5. declaration terminated incorrectly
    By Clane in forum C++ Programming
    Replies: 4
    Last Post: 03-08-2002, 12:09 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21