Friend Operator C++

reverend boltron

Senior member
Nov 18, 2004
945
0
76
Okay, so I'm programming a binary tree in C++, and I'm having some problems with it.

We were working on this in class, and I think I saved an earlier version of it and emailed that one to myself, not knowing that this version had bugs in it. In any event, fast-forward to today, I'm working on my final for the class, and I want to use the binary tree in it, but I keep on getting this error that I don't know how to handle.

The error says ": error C2248: 'Root' : cannot access private member declared in class 'BinaryTree<class TestData>'" .. if anyone has any insights, that would help me out tremendously. Thanks again!
 

juiio

Golden Member
Feb 28, 2000
1,433
4
81
That code compiles fine for me. What line is your error in? Are you sure you pasted the part of the code that errors?
 

reverend boltron

Senior member
Nov 18, 2004
945
0
76
Hm, well, I'm using a test file too. It's a file that the teacher is using to test for errors. I'll post that too.
 

reverend boltron

Senior member
Nov 18, 2004
945
0
76
Yeah, bump for frustration. Seriously. It works when I compile it alone, but that's because it isn't working with the test program and it isn't testing the overloaded << which is giving me the errors. Seriously, anybody with an idea, let me know.
 

beggerking

Golden Member
Jan 15, 2006
1,703
0
0
try pull node class out of the tree class, its a better programming technique to not to define a class within one another.
make node private, add functions to enable binarytree to do operations on node

in constructor of the node class, you should set root pointer to itself if left && right pointers point to null

subroot doesn't look to be defined..


ahh.. let me install an IDE so I can better see the problem...
 

reverend boltron

Senior member
Nov 18, 2004
945
0
76
So I can get it to work when I make everything public. I pulled the Node class out, and I threw on a template to it too, because it needed one for the Data storage. But when I try to make it private, it just doesn't work. I don't get it, and I'm having a lot of trouble understanding this friend command. I don't know. I've been working on it for 15 hours, I need to sleep.

I'll bump this in the morning.. but in the mean time.. if anyone has any suggestions, please, feel free to post.
 

beggerking

Golden Member
Jan 15, 2006
1,703
0
0
haha.. thanks :)

ya, I remember this when I was in school..
have a good sleep.. i'll look into this hopefully I'll be able to help you out..
 

EagleKeeper

Discussion Club Moderator<br>Elite Member
Staff member
Oct 30, 2000
42,589
5
0
Private means that no-one can get to the members.

Protected is for those classes that are derived from the class.

Friend allows other classes not derived to get to the protected/private areas.
 

beggerking

Golden Member
Jan 15, 2006
1,703
0
0
hay, I scanned the whole section from my text book.. go download at
treenode code sample

Note:

treenode give friendship to tree, therefore tree can access private members in the treenode class.
 

SelArom

Senior member
Sep 28, 2004
872
0
0
www.djselarom.com
Originally posted by: EagleKeeper
Private means that no-one can get to the members.

Protected is for those classes that are derived from the class.

Friend allows other classes not derived to get to the protected/private areas.

I kind of remember this... but how do you declare a function to be a friend and why would you want to do that in the first place?

haven't taken data structures in a while so help me refresh my memory :D

-SelArom
 

itachi

Senior member
Aug 17, 2004
390
0
0
Originally posted by: SelArom
Originally posted by: EagleKeeper
Private means that no-one can get to the members.

Protected is for those classes that are derived from the class.

Friend allows other classes not derived to get to the protected/private areas.

I kind of remember this... but how do you declare a function to be a friend and why would you want to do that in the first place?

haven't taken data structures in a while so help me refresh my memory :D

-SelArom
friendship is not inherited. when you declare something as a friend the compiler will treat that symbol as if it were being declared inside the class, without actually declaring it inside the class.
you do this when you want an instance of one object to have access to the members of another. operator overloading in c++ can be done in one of two ways.. either you declare it inside the class, i.e. Object1 & operator=(Object2 &);.. or outside, i.e. Object1 & operator=(Object1 &, Object2&);. when Object2 is your type and you don't want any other objects to access that method, you make it a friend.
and it's not data structures.. it's language.

btw.. your friend declaration is wrong.
friend ostream & operator << <Type> (ostream & o, ...);
only for the declaration tho.
 

reverend boltron

Senior member
Nov 18, 2004
945
0
76
Yeah, I'm still stuck. I know it's the friend part that's getting me. When I make everything public, then it works just fine and dandy, but I'm not supposed to have it that way, and I can't use the BinaryTree that beggerking gave me. So yeah, I'm pretty much giving up. This is driving me nuts, and on the verge of exploding. /rant
 

beggerking

Golden Member
Jan 15, 2006
1,703
0
0
why can't you use it?.. its pretty much a generic binarytree class....Did you run into problem or?
 

beggerking

Golden Member
Jan 15, 2006
1,703
0
0
umm.. how is the one in class different from the one in the book?

the node part should be the same...is that correct?

tree part should be similiar with only different properties/member functions

or are you talking about the seperate header file? you could easily copy / past node class to the tree class header file and it' should work just fine...

if you need help, try post your newest code ( the one which works in public, don't work otherwise)
 

beggerking

Golden Member
Jan 15, 2006
1,703
0
0
did you declare subroot pointer? Don't see it in your code..

but anyways,, try comment in the private
then add to the node class



template <class Type>
class Node
{
friend class Binarytree<Type>;
public:
Node(){Left = NULL; Right = NULL;}
~Node(){ if (Left != NULL) delete Left; if (Right != NULL) delete Right;}
Node<Type>* Left;
Node<Type>* Right;
Node<Type>* Root;
Type data;
};
 

reverend boltron

Senior member
Nov 18, 2004
945
0
76
Good times! Dude, it worked! I had to take off the <Type> from "friend class BinaryTree". Man, seriously, I had tried so many similar things similar to that and I don't know why it didn't work. Man, thank you so much. You're the best. :thumbsup: