Please Help!! I can't figure out how to build the simplest class

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • partybob99

    Please Help!! I can't figure out how to build the simplest class

    I am using Microsoft Visual C++ v1.52. I am trying to build the most
    simple class known to mankind, and I cant get it to compile no matter
    what I do. I am building the program as a .LIB file and not an .EXE.

    I've taken the code from a tutorial manual so the syntax should be
    correct.

    Here is the code:

    class Cat // declare the class object
    {
    public: // members which follow are public
    int itsAge;
    int itsWeight;
    };

    void main()
    {
    Cat Frisky;
    Frisky.itsAge = 5; // assign to the member variable
    }

    I take this code and paste it into a new .c file and then try to
    build. I get the following errors:

    error C2282: 'class' is followed by 'Cat' (missing ','?)
    error C2065: 'Cat' : undeclared identifier
    error C2146: syntax error : missing ';' before identifier 'Frisky'
    error C2065: 'Frisky' : undeclared identifier
    error C2224: left of '.itsAge' must have class/struct/union type

    Does anyone know what is going on here? I know "class" is a valid
    keyword, but it's like the compiler does not that it's a valid
    keyword. I apologize to everyone for being such a moron, but I need
    assistance.

    Thanks in advance.
    Partybob99
  • Julián Albo

    #2
    Re: Please Help!! I can't figure out how to build the simplest class

    partybob99 escribió:
    [color=blue]
    > I take this code and paste it into a new .c file and then try to[/color]

    That way you are compiling it as a C program, not C++. Use a .cpp file
    instead.

    Regards.

    Comment

    • Christopher Benson-Manica

      #3
      Re: Please Help!! I can't figure out how to build the simplest class

      partybob99 <partybob99@hot mail.com> spoke thus:
      [color=blue]
      > void main()[/color]

      Regardless of whether you're compiling in C or C++ mode, main() must return an
      int. void main() is a non-standard prototype and you shouldn't use it. The
      FAQ may prove to be interesting reading:



      --
      Christopher Benson-Manica | I *should* know what I'm talking about - if I
      ataru(at)cybers pace.org | don't, I need to know. Flames welcome.

      Comment

      • Gary Labowitz

        #4
        Re: Please Help!! I can't figure out how to build the simplest class

        "partybob99 " <partybob99@hot mail.com> wrote in message
        news:df018dc6.0 310170508.53356 691@posting.goo gle.com...[color=blue]
        > I am using Microsoft Visual C++ v1.52. I am trying to build the most
        > simple class known to mankind, and I cant get it to compile no matter
        > what I do. I am building the program as a .LIB file and not an .EXE.
        >
        > I've taken the code from a tutorial manual so the syntax should be
        > correct.
        >
        > Here is the code:
        >
        > class Cat // declare the class object
        > {
        > public: // members which follow are public
        > int itsAge;
        > int itsWeight;
        > };
        >
        > void main()
        > {
        > Cat Frisky;
        > Frisky.itsAge = 5; // assign to the member variable
        > }
        >
        > I take this code and paste it into a new .c file and then try to
        > build. I get the following errors:[/color]

        Try naming your file .cpp and recompile. You are telling VC++ to compile C
        code.
        [color=blue]
        >
        > error C2282: 'class' is followed by 'Cat' (missing ','?)
        > error C2065: 'Cat' : undeclared identifier
        > error C2146: syntax error : missing ';' before identifier 'Frisky'
        > error C2065: 'Frisky' : undeclared identifier
        > error C2224: left of '.itsAge' must have class/struct/union type
        >
        > Does anyone know what is going on here? I know "class" is a valid
        > keyword, but it's like the compiler does not that it's a valid
        > keyword. I apologize to everyone for being such a moron, but I need
        > assistance.[/color]

        --
        Gary


        Comment

        Working...