declaring a class

This is a discussion on declaring a class within the C++ Programming forums, part of the General Programming Boards category; Hello, I know this may sound a little bit "newbie-ish" but please bear with me. I've just started out to ...

  1. #1
    Unregistered
    Guest

    declaring a class

    Hello,

    I know this may sound a little bit "newbie-ish" but please bear with me. I've just started out to learn C++, and I have run into a road block trying to declare my own class. I keep getting errors when i go to compile my program. When i am defining the class, and what methods it will use, it won't let me then define those class methods later on......uhh, let me try a little example to explain.


    class Cat
    {
    public:
    //constructors
    Cat(int age);
    ~Cat();

    //accessors
    int GetAge() const {return itsAge;}
    void SetAge(int newAge) {itsAge=newAge;}

    void Meow();

    private:
    int itsAge;
    }

    //now define class methods

    Cat::Cat(int age)
    {
    itsAge=age;
    };

    Cat::~Cat() {};

    void Cat::Meow()
    {
    cout >> "Meow" << endl;
    };


    Ok, so when i go to compile this, i get errors where i try and define my class methods. It seems to be having hard time figuring between my declaration and definitions? im not sure.

    Anyone else have similar problems when learning?

  2. #2
    Unregistered
    Guest
    I think you have to make your constructor and destructor private methods.

  3. #3
    Registered User skyline's Avatar
    Join Date
    Dec 2001
    Posts
    49
    this is an infamous syntax error. you forgot the semicolon after your class declaration.

    [edited]
    example:

    class cat
    {

    //bla bla...

    }; // <<<----- SEMICOLON HERE.
    [/edited]
    Last edited by skyline; 02-14-2002 at 11:50 PM.

  4. #4
    Seņor Member
    Join Date
    Jan 2002
    Posts
    561
    Why do you have to do that?

  5. #5
    Registered User CompiledMonkey's Avatar
    Join Date
    Feb 2002
    Location
    Richmond, VA
    Posts
    438
    Because it's how the language is used. It's like trying to use an integer without delcaring it.

  6. #6
    Seņor Member
    Join Date
    Jan 2002
    Posts
    561
    No I know that it doesn't work without it, but it seems sort of strange that you'd have to do that. Does it have to do with the class's objects, and you need a ; for that? I'm curious, anyone really smart wanna step in?

  7. #7
    Registered User skyline's Avatar
    Join Date
    Dec 2001
    Posts
    49
    same reason why you place one after a declaration of a built-in type (e.g. int x; )

    try looking at:

    class cat
    {
    //bla...
    };

    AS

    class cat {//bla...};

    we just use the first example cuz it looks neater.

  8. #8
    UNBANNED OneStiffRod's Avatar
    Join Date
    Jan 2002
    Posts
    669
    You need it becuase a class is really like a grouping.

    Like an array: int i[] = {1,2,3};
    Like an enumerator: enum{1,2,3};

    Like a struct: struct {.....};

    class {....};

    Like a virgin...{}
    My Avatar says: "Stay in School"

    Rocco is the Boy!
    "SHUT YOUR LIPS..."

  9. #9
    Seņor Member
    Join Date
    Jan 2002
    Posts
    561
    Got it.

  10. #10
    Unregistered
    Guest

    thanks

    thanks alot everyone

  11. #11
    Budding Synth Programmer samGwilliam's Avatar
    Join Date
    Feb 2002
    Location
    Trefforest
    Posts
    368
    Originally posted by Unregistered
    I think you have to make your constructor and destructor private methods.
    How could you access the constructor?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Class design problem
    By h3ro in forum C++ Programming
    Replies: 10
    Last Post: 12-19-2008, 09:10 AM
  2. Replies: 4
    Last Post: 07-10-2006, 03:44 AM
  3. Replies: 3
    Last Post: 10-31-2005, 12:05 PM
  4. Declaring an instance of a class inside a class
    By nickodonnell in forum C++ Programming
    Replies: 4
    Last Post: 10-02-2005, 12:46 AM
  5. Abstract class problem
    By VanJay011379 in forum C++ Programming
    Replies: 9
    Last Post: 07-31-2002, 02:30 PM

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