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

C++ Classes

Options
  • 26-02-2006 5:50pm
    #1
    Closed Accounts Posts: 114 ✭✭


    I'm having trouble making classes with C++. I keep removing this, putting in that and the errors come up and down. Some times, when I compile the exact same thing twice, I get a different number of errors.

    Here:
    #include <iostream.h>
    #include <stdlib.h>
    
    
    class Person;
     {
      unsigned int IQ;
      unsigned int Weight;
      unsigned int Age;
     }
    
    int main()
    {
      Person Alan;
      Person Jonny;
      Alan.IQ = 104;
      Alan.Weight = 116;
      Alan.Age = 32;
      Jonny.IQ = 110;
      Jonny.Weight = 128;
      Jonny.Age = 39;
      cout << "Alan's IQ is " << Alan.IQ << " while Jonny's IQ is " << Jonny.IQ << ".\n";
      cout << "Alan's weight is " << Alan.Weight << " Pounds, while Jonny's Weight is " << Jonny.Weight << ".\n";
      cout << "Alan is " << Alan.Age << "while Jonny is " << Jonny.Age << ".\n";
      system("PAUSE");
      return 0;
    }
    

    I get errors here:

    Parse errors before '{' and '}'
    class Person;[i] //For some reason, by putting in a semicolon, I get less errors[/i]
     {
      unsigned int IQ;
      unsigned int Weight;
      unsigned int Age; 
     } [i]//I don't understand the parse errors stuff =/[/i]
    



    I get errors here because the class is incomplete:
    Person Alan;
      Person Jonny;
    

    Thanks in advance.


Comments

  • Registered Users Posts: 9,480 ✭✭✭projectmayhem


    class X
    {
    public:
    
    private:
    
    };
    

    is the usual way of doing things. i'm no c++ expert though so i may be leading you astray


  • Closed Accounts Posts: 2,349 ✭✭✭nobodythere


    I don't know much about classes but first of all take that semicolon out.

    Secondly AFAIK classes do need that whole public/private thing. You might be confusing 'struct' and 'class'. If you changed the class thing to something like:
    struct Person
    {
      unsigned int IQ;
      unsigned int Weight;
      unsigned int Age;
    }
    
    

    Also as I said I know little about classes but I think you have to use the "New" operator to declare one.

    I dunno, sorry if this isn't helpful






  • Closed Accounts Posts: 114 ✭✭Linkdude


    It turns out it did require public to be used, something I didn't know because the tutorial didn't tell me, I'm not using that again.
    I don't know much about classes but first of all take that semicolon out.

    The compiler kept telling me a semicolon was expected after Person. I took it out now, of course.

    Thanks for the help! :D


  • Registered Users Posts: 1,272 ✭✭✭i_am_dogboy


    grasshopa wrote:
    Also as I said I know little about classes but I think you have to use the "New" operator to declare one.
    You don't have to use the new operator, declaring them as Linkdude did creates the objects on the stack, using the new operator creates the object on the heap.


  • Closed Accounts Posts: 223 ✭✭Chris P Duck


    class Person
    {
    unsigned int IQ;
    unsigned int Weight;
    unsigned int Age;

    Person(){}; // constructor
    ~Person(){}; // destructor
    }


  • Advertisement
  • Registered Users Posts: 9,480 ✭✭✭projectmayhem


    grasshopa wrote:
    I don't know much about classes but first of all take that semicolon out.

    you need that semicolon actually

    proof: http://www.cs.wisc.edu/~hasti/cs368/CppTutorial/NOTES/CLASSES-INTRO.html


  • Closed Accounts Posts: 49 boyracer87


    I think you need the semicolon after the curly brackets and not after "Person" and change to struct:

    struct Person
    {
    unsigned int IQ;
    unsigned int Weight;
    unsigned int Age;
    };


Advertisement