Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

more class problems C++

  • 30-04-2003 1:51pm
    #1
    Moderators, Arts Moderators, Recreation & Hobbies Moderators, Sports Moderators Posts: 9,588 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,317 ✭✭✭✭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,588 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