• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Friend Operator C++

reverend boltron

Senior member
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!
 
That code compiles fine for me. What line is your error in? Are you sure you pasted the part of the code that errors?
 
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.
 
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...
 
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.
 
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..
 
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.
 
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 😀

-SelArom
 
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 😀

-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.
 
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
 
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)
 
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;
};
 
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:
 
Back
Top