Thread: Class help please.

  1. #1
    1479
    Join Date
    Aug 2003
    Posts
    253

    Arrow Class help please.

    I started learning class's and I made this one program and it had so many errors on it. I managed to get them down to 2. I can't seem to figure out what the problem is though. Can anyone help me out ?
    Code:
    #include <iostream>
    using namespace std;
    
    
    class Cat
    {
    public:
        int setAge(int age);
        int setWeight();
        int getAge();
        int getWeight();
        void Meow();
    private:
        int itsAge;
        int itsWeight;
    };
    //=====Starting of Age=========//
    int Cat::setAge(int age)
        {
        itsAge = age;
        }
    int Cat::getAge()
        {
        return itsAge;
        }
    //=========End of Age===========//
    
    
    //=========Meow()=============//
    void Cat::Meow()
        {
        cout <<"Meow" <<endl;
        }
    //======End of Meow============//
    
    
    
    //======Starting Weight==========//
    int Cat::setWeight(int weight)
        {
        itsWeight = weight;
        }
    int Cat::getWeight()
        {
        return itsWeight;
        }
    //==========End of Weight===========//
    
    //===-=-=-=-=-=-=-=-=-Starting Main=-=-=-=-=-=-=-=-=//
    int main()
    {
    int age,weight;
    
    cout <<"How old is your cat?" <<endl;
    cin >>age;
    cout <<"How much does your cat weigh in pounds?" <<endl;
    cin >>weight;
    
    Cat Frisky;
    Frisky.setAge(age);
    Frisky.Meow();
    cout <<"The cat is " << Frisky.getAge() <<" years old." <<endl;
    Frisky.Meow();
    Frisky.setWeight(weight);
    cout <<"The cat is " <<Frisky.getWeight() <<" pounds." <<endl;
    Frisky.Meow();
    return 0;
    }
    Knowledge is power and I want it all

    -0RealityFusion0-

  2. #2
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    Its because your setWeight function is listed as having no parameters in the class declaration, but in the function definition it has the parameter int weight.

    By the way, in the future if you have problems like this, write down the error your compiler gives as well as the code. Many people won't spend the time looking at the code trying to figure out on their own what the bug is.

  3. #3
    1479
    Join Date
    Aug 2003
    Posts
    253
    Ok, thanks for the tip! The code worked too!
    Knowledge is power and I want it all

    -0RealityFusion0-

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    249
    Also, setAge needs to be void because your're not returning anything, and so does setWeight.
    Code:
    #include <iostream>
    using namespace std;
    
    
    class Cat
    {
    public:
        void setAge(int age);
        void setWeight(int weight);
        int getAge();
        int getWeight();
        void Meow();
    private:
        int itsAge;
        int itsWeight;
    };
    //=====Starting of Age=========//
    void Cat::setAge(int age)
        {
        itsAge = age;
        }
    int Cat::getAge()
        {
        return itsAge;
        }
    //=========End of Age===========//
    
    
    //=========Meow()=============//
    void Cat::Meow()
        {
        cout <<"Meow" <<endl;
        }
    //======End of Meow============//
    
    
    
    //======Starting Weight==========//
    void Cat::setWeight(int weight)
        {
        itsWeight = weight;
        }
    int Cat::getWeight()
        {
        return itsWeight;
        }
    //==========End of Weight===========//
    
    //===-=-=-=-=-=-=-=-=-Starting Main=-=-=-=-=-=-=-=-=//
    int main()
    {
    int age,weight;
    
    cout <<"How old is your cat?" <<endl;
    cin >>age;
    cout <<"How much does your cat weigh in pounds?" <<endl;
    cin >>weight;
    
    Cat Frisky;
    Frisky.setAge(age);
    Frisky.Meow();
    cout <<"The cat is " << Frisky.getAge() <<" years old." <<endl;
    Frisky.Meow();
    Frisky.setWeight(weight);
    cout <<"The cat is " <<Frisky.getWeight() <<" pounds." <<endl;
    Frisky.Meow();
    return 0;
    }
    Well, there are a few things wrong with your code:

    1) It does not work.
    2) It does not work.
    3) It does not work.

    Hope this helps.

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. Two conceptual questions
    By AntiScience in forum C++ Programming
    Replies: 3
    Last Post: 11-01-2007, 11:36 AM
  3. Defining derivated class problem
    By mikahell in forum C++ Programming
    Replies: 9
    Last Post: 08-22-2007, 02:46 PM
  4. matrix class
    By shuo in forum C++ Programming
    Replies: 2
    Last Post: 07-13-2007, 01:03 AM