problem...

This is a discussion on problem... within the C++ Programming forums, part of the General Programming Boards category; i am just learning how to do classes, and i made this program, but when i try to compile, it ...

  1. #1
    Unregistered
    Guest

    problem...

    i am just learning how to do classes, and i made this program, but when i try to compile, it gives me an "declaration terminated incorrectly". what am i doing wrong?
    Code:
    #include <iostream.h>
    void public::sayhello()
    {
        cout<<"hello there!";
    }
    void public::saygoodbye()
    {
        cout<<"goodbye!";
    }
    int main()
    {
    int choice
    cout<<"choose one:";
    cout<<"[1] hello";
    cout<<"[2] goodbye";
    cin>>choice;
    if(choice==1)
    {
       sayhello();
    }
    if(choice==2)
    {
       saygoodbye();
    }
    return 0;
    }

  2. #2
    Unregistered
    Guest
    int choice

    should be

    int choice;

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    drop the public:: in both of the following two lines:

    void public::sayhello()

    void public::saygoodbye()

    When that is done you will have an example of a program that uses two simple functions and uses a simple menu to allow user to make a single choice each time the program is done. Unfortunately, this program has little to do with classes. To have the two functions belong to a class you would need to do something like this:

    Code:
    #include <iostream.h>
    
    //declare the class 
    class communicate
    {
    public:
      void sayhello();
      void saygoodbye();
    };
    
    
    //provide member function definitions
    void  communicate::sayhello()
    {
        cout<<"hello there!";
    }
    void communicate::saygoodbye()
    {
        cout<<"goodbye!";
    }
    
    
    int main()
    {
    int choice;
    communicate talk;
    cout<<"choose one:";
    cout<<"[1] hello";
    cout<<"[2] goodbye";
    cin>>choice;
    if(choice==1)
    {
       talk.sayhello();
    }
    if(choice==2)
    {
       talk.saygoodbye();
    }
    char dummy;
    cin >> dummy;
    return 0;
    }

    At some point you will want to start putting class definitions in your own header files so you can reuse classes in more than one program, but based on the code as posted, I think there are a few more lessons to be learned between now and then.
    Last edited by elad; 03-18-2002 at 07:36 PM.

  4. #4
    Unregistered
    Guest

    Talking thanks, but...

    uh, ok. i guess i didnt have that clear of an idea of what a class is. removing the public:: works fine, however, your second one produces a whole bunch of error messages in Borland C++ compiler 5.5.

    thanks anyways!

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    I corrected the mistake in the original post for the class example. It runs fine now in BCB as a console program.

    //declare class class communicate

    should have been

    //declare class
    class communicate

    Sorry

  6. #6
    Unregistered
    Guest
    ohh, i see now, you had it on the same line so it just read as a comment! Now why didnt _i_ catch that?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 05:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 09:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 03:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 07:54 PM

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