Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

more class problems C++

  • 30-04-2003 02:51PM
    #1
    Moderators, Arts Moderators, Recreation & Hobbies Moderators, Sports Moderators Posts: 9,760 Mod ✭✭✭✭


    Am I doing something reall stupid and wrong here?

    These are the errors I'm getting...I'm looking on the web and cannot find any reason for my errors.






    Configuration: practice_classes - Win32 Debug
    Compiling...
    practice_classes.cpp
    C:\Program Files\Microsoft Visual Studio\MyProjects\practice_classes\practice_classes.cpp(14) : error C2599: 'get_input' : local class member functions must be defined within the class
    C:\Program Files\Microsoft Visual Studio\MyProjects\practice_classes\practice_classes.cpp(14) : see declaration of 'get_input'
    C:\Program Files\Microsoft Visual Studio\MyProjects\practice_classes\practice_classes.cpp(15) : error C2599: 'display_input' : local class member functions must be defined within the class
    C:\Program Files\Microsoft Visual Studio\MyProjects\practice_classes\practice_classes.cpp(15) : see declaration of 'display_input'
    C:\Program Files\Microsoft Visual Studio\MyProjects\practice_classes\practice_classes.cpp(18) : error C2144: syntax error : missing ')' before type 'void'
    C:\Program Files\Microsoft Visual Studio\MyProjects\practice_classes\practice_classes.cpp(18) : error C2352: 'main::info::get_input' : illegal call of non-static member function
    C:\Program Files\Microsoft Visual Studio\MyProjects\practice_classes\practice_classes.cpp(14) : see declaration of 'get_input'
    C:\Program Files\Microsoft Visual Studio\MyProjects\practice_classes\practice_classes.cpp(18) : error C2059: syntax error : ')'
    C:\Program Files\Microsoft Visual Studio\MyProjects\practice_classes\practice_classes.cpp(19) : error C2143: syntax error : missing ';' before '{'
    C:\Program Files\Microsoft Visual Studio\MyProjects\practice_classes\practice_classes.cpp(20) : error C2065: 'character' : undeclared identifier
    C:\Program Files\Microsoft Visual Studio\MyProjects\practice_classes\practice_classes.cpp(21) : error C2065: 'integer' : undeclared identifier
    C:\Program Files\Microsoft Visual Studio\MyProjects\practice_classes\practice_classes.cpp(24) : error C2144: syntax error : missing ')' before type 'void'
    C:\Program Files\Microsoft Visual Studio\MyProjects\practice_classes\practice_classes.cpp(24) : error C2352: 'main::info::display_input' : illegal call of non-static member function
    C:\Program Files\Microsoft Visual Studio\MyProjects\practice_classes\practice_classes.cpp(15) : see declaration of 'display_input'
    C:\Program Files\Microsoft Visual Studio\MyProjects\practice_classes\practice_classes.cpp(24) : error C2059: syntax error : ')'
    C:\Program Files\Microsoft Visual Studio\MyProjects\practice_classes\practice_classes.cpp(25) : error C2143: syntax error : missing ';' before '{'
    Error executing cl.exe.



    #include<iostream.h>
    #include<conio.h>
    int main()
    {


    class info
    {
    private:
    char character;
    int integer;

    public:
    get_input(void);
    display_input(void);
    };

    info::get_input(void)
    {
    cin>>character;
    cin>>integer;
    }

    info::display_input(void)
    {
    cout<<character;
    cout<<integer;
    }

    getch();
    return 0;
    }


Comments

  • Registered Users, Registered Users 2 Posts: 68,173 ✭✭✭✭seamus


    You have your class within your main method. That's not the way it works :)

    Your main method is what is executed when you run the program. A class is a set of parameters and functions that allow you to do stuff to an object *within* the main method.

    In the main method you want to execute instructions in a sequential order, and produce some output. When you write a class, you are declaring *how* functions operate, and *how* to produce your output, but you're not actually doing it.

    If that makes any sense. :)


  • Moderators, Arts Moderators, Recreation & Hobbies Moderators, Sports Moderators Posts: 9,760 Mod ✭✭✭✭BossArky


    Agh, ok nice one, it works now :)

    another questions...when I use getch() it often works correctly...however in the program below it doesn't wait for a keystroke...I suppose this is because it is after main's return 0;....but I had it before the return 0; initially and waited for a keystroke..then output the data and quit rapidly without giving me time to observe the screen.

    Where exactly should I put getch() in a program like this?





    #include<iostream.h>
    #include<conio.h>



    class info
    {
    private:
    char character;
    int integer;

    public:
    get_input(void);
    display_input(void);
    };

    int main()
    {

    info stuff;
    stuff.get_input();
    stuff.display_input();


    return 0;
    getch();
    }


    info::get_input(void)
    {
    cout<<"Enter char"<<endl;
    cin>>character;
    cout<<"Enter int"<<endl;
    cin>>integer;
    }

    info::display_input(void)
    {
    cout<<"\nChar was: ";
    cout<<character;
    cout<<"\nInt was: ";
    cout<<integer;
    }


  • Closed Accounts Posts: 1,651 ✭✭✭Enygma


    The getch() in the above code won't be executed as it's after the return 0;

    You could do something like this:

    char dummy;

    .
    .
    .
    int main()
    {
    .
    .
    // code to be executed
    cin >> dummy; // waits for input

    return 0;
    }


Advertisement