Thread: Refrences and a dog named skinbag.

  1. #1
    Registered User CoderMatt's Avatar
    Join Date
    Feb 2003
    Posts
    3

    Angry Refrences and a dog named skinbag.

    Hey...I need help.

    I was atempting to pass a reference through a class method and then use the method to access the private integer 'age' with the reference. This isn't for a program but I need to understand this. Any help would be apreciated.

    My compiler is :MSVC++ 6...the error is located at the bottom. What I need to know is how to access the private int age using a reference...thanks.

    #include <iostream.h>
    class dog //Telling compiler what a dog is..
    {
    public:
    int getAge;
    void setAge(int &rAge) //passing a reference
    {
    rAge=age; //declaring the reference to age.
    }


    private:
    int age;
    };

    int main()
    {
    dog SkinBag; //Making my dog...
    SkinBag.setAge(5); //error line ... Setting skinbags age

    return 0;
    }


    error:
    D:\Vc98\playingaround.cpp(19) : error C2664: 'setAge' : cannot convert parameter 1 from 'const int' to 'int &'
    A reference that is not to 'const' cannot be bound to a non-lvalue

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    Your code has a few mistakes... heres how it should look....
    Code:
    class dog
    {
       public:
           dog() : Age(0) {}
           dog(int age) : Age(age) {}
           int GetAge() const { return Age;}
           void SetAge(const int age) { Age = age;}
       private:
           int Age;
    }
    
    #include <iostream>
    using std::cout;
    using std::endl;
    
    int main()
    {
       dog Fred;
       dog Joe(10);
       cout << Joe.GetAge()<<endl;
       Fred.SetAge(15);
       cout << Fred.GetAge()<<endl;
       Joe = Fred; // uses compiler provided default copy assignment operator.
       cout << Joe.GetAge()<<endl;
       return 0;
    }
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    Registered User CoderMatt's Avatar
    Join Date
    Feb 2003
    Posts
    3

    thanks..but...

    Thank you for responding and helping so quick. As I said though the code is not for a program but for a better understanding of incorperating references with oop class structure and such. Thank you for taking the time to right the code too, unfortunitly the problem still stands, I want to somehow incorperate references into the program.

  4. #4
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    The short answer is you dont. For passing an int a reference is less efficient than passing by value. But if you must use a reference change this...

    void SetAge(const int age) { Age = age;}

    to

    void SetAge(const int& age) { Age = age;}

    and this

    int GetAge() const { return Age;}

    to

    const int& GetAge() const { return Age;}
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  5. #5
    Registered User CoderMatt's Avatar
    Join Date
    Feb 2003
    Posts
    3

    thanks

    Thank you thats all I asked for.

Popular pages Recent additions subscribe to a feed