C++ friend function

Discussion in 'C++' started by Der Andere, Apr 13, 2004.

  1. Der Andere

    Der Andere Guest

    I've got a problem concerning the << operator. Although I've declared it as
    a friend function it cannot access the private member of the class AVLtree.
    Some code:

    ------------------
    class AVLtree
    {
    public:
    friend ostream& operator << (ostream& os, AVLtree& t){};
    .... some unimportant stuff ...
    private:
    Tree tree;
    };

    ostream& operator << (ostream& os, AVLtree& t)
    {
    // if (t.tree!=NULL)
    os << *(t.tree);
    return os;
    }
    ----------------

    I cannot figure out why the compiler tells

    error C2248: 'tree' : cannot access private member declared in class
    'AVLtree'

    Any advice?

    Thanks in advance,
    Matthias

    --
    Für emails Anweisung in der Adresse befolgen
     
    Der Andere, Apr 13, 2004
    #1
    1. Advertising

  2. Der Andere wrote:

    > I've got a problem concerning the << operator. Although I've declared it as
    > a friend function it cannot access the private member of the class AVLtree.
    > Some code:


    When posting code, please try to make it compilable. This is FAQ 5.8.

    >
    > ------------------
    > class AVLtree
    > {
    > public:
    > friend ostream& operator << (ostream& os, AVLtree& t){};


    What's with the '{}'? I don't even know if that's legal, and it
    certainly seems nonsensical. Are you defining the function here, or are
    you defining it later?

    > ... some unimportant stuff ...
    > private:
    > Tree tree;
    > };
    >
    > ostream& operator << (ostream& os, AVLtree& t)
    > {
    > // if (t.tree!=NULL)
    > os << *(t.tree);
    > return os;
    > }


    -Kevin
    --
    My email address is valid, but changes periodically.
    To contact me please use the address from a recent posting.
     
    Kevin Goodsell, Apr 13, 2004
    #2
    1. Advertising

  3. Der Andere

    Buster Guest

    Der Andere wrote:

    > I've got a problem concerning the << operator. Although I've declared it as
    > a friend function it cannot access the private member of the class AVLtree.
    > Some code:
    >
    > ------------------
    > class AVLtree
    > {
    > public:
    > friend ostream& operator << (ostream& os, AVLtree& t){};


    Defined here.

    > ... some unimportant stuff ...
    > private:
    > Tree tree;
    > };
    >
    > ostream& operator << (ostream& os, AVLtree& t)
    > {
    > // if (t.tree!=NULL)
    > os << *(t.tree);
    > return os;
    > }


    And here.

    > ----------------
    >
    > I cannot figure out why the compiler tells
    >
    > error C2248: 'tree' : cannot access private member declared in class
    > 'AVLtree'


    Funny, mine says
    "error: redefinition of `std::eek:stream& operator<<(std::eek:stream&, AVLtree&)".

    > Any advice?


    Post the real code.

    --
    Regards,
    Buster.
     
    Buster, Apr 13, 2004
    #3
  4. Der Andere

    Der Andere Guest

    Ok then, underneath you'll find the complete code, as I got it. It should
    compile now.

    Regards,
    Matthias

    ----------------------

    #include <iostream>
    #include <cassert>
    #include <iomanip>

    using namespace std;

    typedef int T;

    T max(T a, T b)
    {
    return a > b ? a : b ;
    }

    T min(T a, T b)
    {
    return a < b ? a : b ;
    }

    // ----------

    enum Child {Left=0, Right=1};

    Child opposite(Child k)
    {
    return Child(1-k);
    }

    // ----------

    enum Balance { LeftBal=-1, Balanced=0, RightBal=1};

    const bool deeper = true;
    const bool sameDepth = false;

    // ----------

    typedef class Node* Tree;
    const Tree Empty = NULL;

    class Node
    {
    enum { Arity=2 };
    Tree children[Arity];
    int b;
    T e;
    public:
    Node(T & v) : e(v), b(0) { for (int i=0;i<Arity;i+=1) children=Empty; }
    ~Node();

    friend int& bal(Tree p);
    friend T& el (Tree p);
    friend Tree & child(Tree p, Child k);
    };

    int& bal(Tree p) { assert(p); return p->b; }

    T& el (Tree p) { assert(p); return p->e; }

    Tree& child(Tree p, Child k) { assert(p); return p->children[k]; }

    Tree& left(Tree p) { return child(p,Left); }

    Tree& right(Tree p) { return child(p,Right); }

    Node :: ~Node()
    {
    for (int i=0;i<Arity;i+=1)
    if (children)
    delete children;
    }

    // ----------

    void rotate (Tree & root, Child c)
    {
    Child other = opposite(c);
    assert(child(root,other)); // other Child should exist
    Tree oldRoot = root;

    root = child(root,other);
    child(oldRoot,other) = child(root,c);
    child(root,c) = oldRoot;

    if (c==Left)
    {
    bal(oldRoot) -= 1+ max(bal(root),0);
    bal(root) -= 1- min(bal(oldRoot),0);
    }
    else
    {
    bal(oldRoot) += 1- min(bal(root),0);
    bal(root) += 1+ max(bal(oldRoot),0);
    }
    }

    void rotateDubbel (Tree & root, Child c)
    {
    Child other = opposite(c);
    rotate(child(root,other),other);
    rotate(root,c);
    }

    // ----------

    bool insertRight(Tree& tree, T v);
    bool insertLeft(Tree& tree, T v);

    bool insert(Tree& tree, T v) // returns true if tree becomes deeper
    {
    if (tree==Empty)
    {
    tree = new Node (v);
    return deeper;
    }
    else if (v==el(tree))
    return sameDepth;
    else if (v<el(tree))
    return insertLeft (tree,v);
    else
    return insertRight (tree,v);
    }

    bool insertRight(Tree& tree, T v)
    {
    if (insert(right(tree),v))
    {
    bal(tree) += 1;
    switch(bal(tree))
    {
    case 2:
    if (bal(right(tree)) == LeftBal)
    rotateDubbel(tree,Left);
    else
    rotate(tree,Left);
    return sameDepth;
    case 1: return deeper;
    case 0: return sameDepth;
    default: assert(false);
    }
    }
    else
    return sameDepth;
    }

    bool insertLeft(Tree& tree, T v)
    {
    if (insert(left(tree),v))
    {
    bal(tree) -= 1;
    switch(bal(tree))
    {
    case -2:
    if (bal(left(tree)) == RightBal)
    rotateDubbel(tree,Right);
    else
    rotate(tree,Right);
    return sameDepth;
    case -1: return deeper;
    case 0: return sameDepth;
    default: assert(false);
    }
    }
    else
    return sameDepth;
    }

    bool contains (Tree tree, T v)
    {
    if (tree == Empty)
    return false;
    else if (el(tree) == v)
    return true;
    else if (el(tree) < v)
    return contains(right(tree),v);
    else
    return contains(left(tree),v);
    }

    bool sorted (Tree tree, T* v=NULL)
    {
    if (tree==Empty)
    return true;
    else
    {
    bool sl = sorted(left(tree),v);
    if (v && el(tree) <= *v)
    {
    sl = false;
    cout << "element " << *v << " staat op de verkeerde plaats\n";
    }
    v = &(el(tree));
    return sorted(right(tree),v) && sl;
    }
    }

    bool balanced (Tree tree, int& d)
    {
    if (tree==Empty)
    {
    d = 0;
    return true;
    }
    else
    {
    int l=0, r=0;
    bool bl = balanced(left(tree),l);
    bool br = balanced(right(tree),r);
    if (bal(tree) != r-l)
    cout << "balansfactor in knoop met " << el(tree) << " verkeerd\n";
    if (abs(r-l)>1)
    cout << "avl tree is op knoop met " << el(tree) << " uit balans\n";
    d = max(l,r) + 1;
    return bl && br && bal(tree) == r-l && abs(r-l)<=1;
    }
    }


    ostream& operator << (ostream& os, Node& n)
    {
    static int indent = 0;
    const int stap = 3;
    indent += stap;
    if (right(&n)!=Empty)
    os << *right(&n);
    os << setw(indent-stap) << "" << el(&n) << " bal=" << bal(&n) << endl;
    if (left(&n)!=Empty)
    os << *left(&n);
    indent -= stap;
    return os;
    }

    // ----------
    // ----------

    class AVLtree
    {
    Tree tree;
    public:
    AVLtree(): tree(Empty) {}
    ~AVLtree() { if (tree) delete tree; }
    friend ostream& operator << (ostream& os, AVLtree& t);
    void Insert(T v) { insert(tree,v); }
    bool Contains(T v) { return contains(tree,v); }
    bool Sorted() { return sorted(tree); }
    bool Balanced() {int d =0; return balanced(tree,d); }
    };

    ostream& operator << (ostream& os, AVLtree& t)
    {
    if (t.tree!=Empty)
    os << *(t.tree);
    return os;
    }


    // ----------

    void main ()
    {
    // int val = 2;
    // Node<int>* ptr = new Node<int>(val);
    // cout << *ptr ;

    /* Node<int>* t = Empty;
    int rij[10] = {0,9,1,2,3,4,5,6,7,8};
    for (int i = 0;i<10;i+=1)
    {
    insert(t,rij);
    cout << *t << endl;
    }
    */

    AVLtree t;
    // int rij[10] = {0,9,1,2,4,4,5,6,7,8};
    // int rij[10] = {5,3,7,2,4,6,1,1,1,1};
    int rij[10] = {4,2,6,1,3,5,7,1,1,1};
    for (int i = 0;i<10;i+=1)
    {
    t.Insert(rij);
    cout << t << endl;
    if (!t.Sorted() || !t.Balanced())
    cout << "Improper AVL tree\n";
    }
    for (int j = -1;j<11;j+=1)
    cout << "contains " << j << " = " << boolalpha << t.Contains(j) << endl;


    }
     
    Der Andere, Apr 13, 2004
    #4
  5. Der Andere

    Buster Guest

    Der Andere wrote:
    > Ok then, underneath you'll find the complete code, as I got it. It should
    > compile now.


    No, I get "error: `main' must return `int'".

    Having fixed that, yes, it does compile. So, what, you fixed it?
    What happened to "error C2248: 'tree' : cannot access private member
    declared in class 'AVLtree'"?

    --
    Regards,
    Buster.
     
    Buster, Apr 13, 2004
    #5
  6. Der Andere

    Der Andere Guest

    > Der Andere wrote:
    > > Ok then, underneath you'll find the complete code, as I got it. It

    should
    > > compile now.

    >
    > No, I get "error: `main' must return `int'".
    >
    > Having fixed that, yes, it does compile. So, what, you fixed it?
    > What happened to "error C2248: 'tree' : cannot access private member
    > declared in class 'AVLtree'"?


    Errh, no, I didn't fix it. I've still got it.
    I've never had this kind of problem with my compiler.
    But if it works on your computer, the fault may be in my system, not in the
    code?!

    Regards,
    Matthias
     
    Der Andere, Apr 13, 2004
    #6
  7. Der Andere

    Buster Guest

    Der Andere wrote:

    > Errh, no, I didn't fix it. I've still got it.
    > I've never had this kind of problem with my compiler.
    > But if it works on your computer, the fault may be in my system, not in the
    > code?!


    What compiler is it, and what version? I gather that with Visual C++,
    anything before 7.1 was pretty crappy. If I see any problems with the
    code you posted I'll let you know.

    --
    Regards,
    Buster.
     
    Buster, Apr 13, 2004
    #7
  8. Der Andere

    Der Andere Guest

    > > Errh, no, I didn't fix it. I've still got it.
    > > I've never had this kind of problem with my compiler.
    > > But if it works on your computer, the fault may be in my system, not in

    the
    > > code?!

    >
    > What compiler is it, and what version? I gather that with Visual C++,
    > anything before 7.1 was pretty crappy.


    Oh, I've got Visual C++ 6.0. So maybe Microsoft is the problem, not me ;-)

    >If I see any problems with the
    > code you posted I'll let you know.


    Thanks!

    Regards,
    Matthias
     
    Der Andere, Apr 13, 2004
    #8
  9. Der Andere

    Leor Zolman Guest

    On Tue, 13 Apr 2004 03:23:17 +0200, "Der Andere" <>
    wrote:

    >> > Errh, no, I didn't fix it. I've still got it.
    >> > I've never had this kind of problem with my compiler.
    >> > But if it works on your computer, the fault may be in my system, not in

    >the
    >> > code?!

    >>
    >> What compiler is it, and what version? I gather that with Visual C++,
    >> anything before 7.1 was pretty crappy.

    >
    >Oh, I've got Visual C++ 6.0. So maybe Microsoft is the problem, not me ;-)


    Anytime the words "friend function", "strange error" and "MSVC 6" are
    uttered together, there's a good chance you're dealing with the infernal
    "friend" bug in VC6. If you haven't yet, go download the latest service
    pack (it is up to Service Pack 6 now) at:

    http://msdn.microsoft.com/vstudio/downloads/updates/sp/vs6/sp6/default.aspx

    -leor


    --
    Leor Zolman --- BD Software --- www.bdsoft.com
    On-Site Training in C/C++, Java, Perl and Unix
    C++ users: download BD Software's free STL Error Message Decryptor at:
    www.bdsoft.com/tools/stlfilt.html
     
    Leor Zolman, Apr 13, 2004
    #9
  10. Der Andere

    Buster Guest

    Der Andere wrote:

    >>What compiler is it, and what version? I gather that with Visual C++,
    >>anything before 7.1 was pretty crappy.

    >
    > Oh, I've got Visual C++ 6.0. So maybe Microsoft is the problem, not me ;-)


    Maybe so. I'm no expert.

    Some things to try, without altering the correctness of your code:

    (i) Add a forward declaration of the << overload before the definition
    of the class of which it is a friend.

    (ii) Define the friend function inside AVL tree class, so that it looks
    a bit like an inline member function definition.

    >>If I see any problems with the
    >>code you posted I'll let you know.

    >
    > Thanks!


    --
    Regards,
    Buster.
     
    Buster, Apr 13, 2004
    #10
  11. Der Andere

    Buster Guest

    Buster wrote:

    > Der Andere wrote:
    >
    >> Oh, I've got Visual C++ 6.0. So maybe Microsoft is the problem, not me
    >> ;-)

    >
    > Maybe so. I'm no expert.


    Looks like we're in the right place. Follow Leor's advice, not mine!

    --
    Regards,
    Buster.
     
    Buster, Apr 13, 2004
    #11
  12. Der Andere

    Der Andere Guest

    > >> > Errh, no, I didn't fix it. I've still got it.
    > >> > I've never had this kind of problem with my compiler.
    > >> > But if it works on your computer, the fault may be in my system, not

    in
    > >the
    > >> > code?!
    > >>
    > >> What compiler is it, and what version? I gather that with Visual C++,
    > >> anything before 7.1 was pretty crappy.

    > >
    > >Oh, I've got Visual C++ 6.0. So maybe Microsoft is the problem, not me

    ;-)
    >
    > Anytime the words "friend function", "strange error" and "MSVC 6" are
    > uttered together, there's a good chance you're dealing with the infernal
    > "friend" bug in VC6. If you haven't yet, go download the latest service
    > pack (it is up to Service Pack 6 now) at:
    >
    >

    http://msdn.microsoft.com/vstudio/downloads/updates/sp/vs6/sp6/default.aspx

    Indeed. That was the problem. I've just installed the service pack and the
    compiler issues no errors :))

    Regards,
    Matthias
     
    Der Andere, Apr 13, 2004
    #12
    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:
    320
    infobahn
    Feb 21, 2005
  2. Replies:
    1
    Views:
    1,416
    Jim Langston
    Nov 9, 2006
  3. ciccio
    Replies:
    4
    Views:
    488
    ciccio
    Jan 18, 2008
  4. Replies:
    2
    Views:
    702
    Triple-DES
    Feb 26, 2008
  5. Peter
    Replies:
    2
    Views:
    312
    Öö Tiib
    Jun 6, 2013
Loading...

Share This Page