simple friend function not working

Discussion in 'C++' started by suresh.amritapuri, Dec 12, 2009.

  1. Hi,
    What is wrong in this simple friend function code?

    #include <iostream>
    using namespace std;

    class B;
    class A{
    public:
    A():aa(10){}
    void dispA(){cout << aa << endl;}
    private:
    int aa;
    friend class B;
    };

    class B{
    public:
    B():bb(20){}
    void dispB(){cout << bb + aa << endl;}
    private:
    int bb;
    };

    int main(){

    A a;
    B b;
    b.dispB();
    }

    compiler (g++) says: testFriend.cpp:17: error: ‘aa’ was not declared
    in this scope.

    any suggestions?
    thanks
    suresh
     
    suresh.amritapuri, Dec 12, 2009
    #1
    1. Advertising

  2. suresh.amritapuri

    Ian Collins Guest

    suresh.amritapuri wrote:
    > Hi,
    > What is wrong in this simple friend function code?
    >
    > #include <iostream>
    > using namespace std;
    >
    > class B;
    > class A{
    > public:
    > A():aa(10){}
    > void dispA(){cout << aa << endl;}
    > private:
    > int aa;
    > friend class B;
    > };
    >
    > class B{
    > public:
    > B():bb(20){}
    > void dispB(){cout << bb + aa << endl;}
    > private:
    > int bb;
    > };
    >
    > int main(){
    >
    > A a;
    > B b;
    > b.dispB();
    > }
    >
    > compiler (g++) says: testFriend.cpp:17: error: ‘aa’ was not declared
    > in this scope.


    That's correct. There isn't a member of B called aa.

    Friendship only grants a class access to another's private parts, it
    doesn't include it.

    --
    Ian Collins
     
    Ian Collins, Dec 12, 2009
    #2
    1. Advertising

  3. suresh.amritapuri

    Jonathan Lee Guest

    On Dec 11, 7:34 pm, "suresh.amritapuri" <>
    wrote:
    > Hi,
    > What is wrong in this simple friend function code?


    You're trying to use it like you were inheriting. "aa"
    is indeed not in scope.

    "aa" is not a member of class B. It is a member of class A
    and, more to the point wrt B, a member of an *instance* of A.

    You would need to change to either:

    // Access the member aa of instance x
    void dispB(const A& x) { cout << bb + x.aa << endl; }

    or drop the friend and inherit A

    class B : public A {
    // ...
    };

    Depends on what you're really trying to do. A third
    option would be to write a getter for aa. i.e.,
    int A::get_aa() const { return aa; } // pick a better name

    --Jonathan
     
    Jonathan Lee, Dec 12, 2009
    #3
  4. On Dec 11, 5:11 pm, Jonathan Lee <> wrote:
    > On Dec 11, 7:34 pm, "suresh.amritapuri" <>
    > wrote:
    >
    > > Hi,
    > > What is wrong in this simple friend function code?

    >
    > You're trying to use it like you were inheriting. "aa"
    > is indeed not in scope.
    >
    > "aa" is not a member of class B. It is a member of class A
    > and, more to the point wrt B, a member of an *instance* of A.
    >
    > You would need to change to either:
    >
    >   // Access the member aa of instance x
    >   void dispB(const A& x) { cout << bb + x.aa << endl; }
    >
    > or drop the friend and inherit A
    >
    >   class B : public A {
    >     // ...
    >   };
    >
    > Depends on what you're really trying to do. A third
    > option would be to write a getter for aa. i.e.,
    >   int A::get_aa() const { return aa; } // pick a better name
    >
    > --Jonathan


    Thank you all, I got the point,
    suresh
     
    suresh.amritapuri, Dec 12, 2009
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. csudha

    Reg virtual function and friend function

    csudha, Feb 20, 2005, in forum: C Programming
    Replies:
    5
    Views:
    314
    infobahn
    Feb 21, 2005
  2. Replies:
    1
    Views:
    1,339
    Jim Langston
    Nov 9, 2006
  3. ciccio
    Replies:
    4
    Views:
    466
    ciccio
    Jan 18, 2008
  4. Replies:
    2
    Views:
    674
    Triple-DES
    Feb 26, 2008
  5. Peter
    Replies:
    2
    Views:
    278
    Öö Tiib
    Jun 6, 2013
Loading...

Share This Page