C ++好友功能 [英] C++ friend function

查看:52
本文介绍了C ++好友功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对<<<<<<<<<运营商。虽然我已将其声明为

a朋友功能,但它无法访问AVLtree类的私人会员。

部分代码:


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

class AVLtree

{

public:

朋友ostream&运算符<< (ostream& os,AVLtree& t){};

....一些不重要的东西...

私人:

树木;

};


ostream&运算符<< (ostream& os,AVLtree& t)

{

// if(t.tree!= NULL)

os<< *(t.tree);

返回操作系统;

}

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


我无法弄清楚为什么编译器会告诉


错误C2248:''tree'':无法访问类中声明的私有成员

''AVLtree''


有什么建议吗?


提前致谢,

Matthias


-
$ b $bFür电子邮件Anweisung in der Adresse befolgen

解决方案

< blockquote> Der Andere写道:

我遇到了关于<<<<运营商。虽然我已经宣布它是朋友的功能,但它无法访问AVLtree类的私人成员。
一些代码:


发布代码时,请尽量使其可编辑。这是FAQ 5.8。

------------------
class AVLtree
{
公开:

'{}''有什么用?我甚至不知道这是否合法,而且它确实看起来毫无意义。你在这里定义这个功能,或者你以后定义它是什么?

...一些不重要的东西......
私人:
树树;
};

ostream&运算符<< (ostream& os,AVLtree& t)
{if /> // if(t.tree!= NULL)
os<< *(t.tree);
返回操作系统;
}




-Kevin

-

我的电子邮件地址有效,但会定期更改。

要联系我,请使用最近发布的地址。


Der Andere写道:

我有一个关于<<<<<<<<<运营商。虽然我已经宣布它是朋友的功能,但它无法访问AVLtree类的私人成员。
一些代码:

--------- ---------
上课AVLtree
{
公众:
朋友ostream&运算符<< (ostream& os,AVLtree& t){};


定义在这里。

......一些不重要的东西......
私人:
树树;
} ;

ostream&运算符<< (ostream& os,AVLtree& t)
{if /> // if(t.tree!= NULL)
os<< *(t.tree);
返回操作系统;
}


这里。

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

我无法弄清楚为什么编译器会告诉错误C2248:''tree'':无法访问类中声明的私有成员
''AVLtree''


好​​笑,我说

"错误:重新定义`std :: ostream& operator<<(std :: ostream&,AVLtree&)"。

有什么建议吗?




发布实际代码。


-

问候,

巴斯特。


好的话下面你会找到完整的代码,因为我得到了它。现在应该编译



问候,

Matthias


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


#include< iostream>

#include< cassert>

#include< iomanip>


使用命名空间std;


typedef int T;


T max(T a,T b)

{

返回> b? a:b;

}


T min(T a,T b)

{

返回< b? a:b;

}


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


enum孩子{左= 0,右= 1};


孩子对面(孩子k)

{

返回孩子(1 -k);

}


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


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


const bool deep = true;

const bool sameDepth = false;


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


typedef类Node * Tree;

const Tree空= NULL;


类Node

{

enum {Arity = 2};

树儿[Arity];

int b;

T e;

public:

节点(T& ; v):e(v),b(0){for(int i = 0; i< Arity; i + = 1)children [i] = Empty; }

~Node();


朋友int& bal(Tree p);

朋友T& el(Tree p);

朋友Tree&孩子(树p,孩子k);

};


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


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


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


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


Tree&对(树p){返回孩子(p,右); }


节点::〜节点()

{

for(int i = 0; i< Arity; i + = 1)

if(children [i])

删除孩子[i];

}


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


void rotate(Tree& root,Child c)

{

Child other = opposite(c);

assert(child(root,other)); //其他孩子应该存在

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-分钟(bal(root),0);

bal(root)+ = 1+ max(bal(oldRoot),0);

}

}


void rotateDubbel(Tree& root,Child c)

{

其他孩子=对面(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)//如果树变得更深,则返回true

{

if(tree == Empty)

{

tree = new节点(v);

返回更深;

}

否则if(v == el(tree))

返回sameDepth;

else if(v< el(tree))

返回insertLeft(tree,v);

else

返回insertRight(树,v);

}


bool insertRight(Tree&树,T v)

{

if(insert(right(tree),v))

{

bal(树)+ = 1;

开关(bal(树))

{

案例2:

if(bal(right(tree))== LeftBal)

rotateDubbel(树,左);

else

rotate(树,左);

返回sameDepth;

案例1:返回更深;

案例0:返回sameDepth;

默认:断言(假);

}

}

其他

返回sameDepth;

}


bool insertLeft(Tree& tree,T v)

{

if(insert(left(left)树),v))

{

bal(树) - = 1;

开关(bal(树))

{

案例-2:

如果(bal(左(树))== RightBal)

rotateDubbel(树,右);

其他

旋转(树,右);

返回sameDepth;

case -1:返回更深;

案例0:返回sameDepth;

默认:断言(false);

}

}

其他

返回sameDepth;

}


bool包含(树树, T v)

{

if(tree == Empty)

返回false;

else if(el (树)== v)

返回true;

else if(el(tree)< v)

return包含(right(tree),v);

else

return contains(left(tree),v); < br $>
}


bool排序(树形树,T * v = NULL)

{

if (树==空)

返回true;

else

{

bool sl = sorted(left(left)树),v);

if(v&& el(tree)< = * v)

{

sl = false;

cout<< 元素 << * v<< " staat op de verkeerde plaats \ n" ;;

}

v =&(el(tree));

返回排序(右(树),v)&& sl;

}

}


bool balanced(树树,整数和广告)

{

如果(树==空)

{

d = 0;

返回true;

}

其他

{

int l = 0,r = 0;

bool bl =平衡(左(树),l);

bool br =平衡(右(树),r);

if(bal(tree)!= rl )

cout<< knoop中的balansfactor遇见 << el(树)<< " verkeerd \ n" ;;

if(abs(r-l)> 1)

cout<< avl tree is op knoop met << el(树)<< " uit balans \ n" ;;

d = max(l,r)+ 1;

返回bl&& br&& bal(树)== r-l&& abs(r-l)< = 1;

}

}

ostream&运算符<< (ostream& os,Node& n)

{

static int indent = 0;

const int stap = 3;

indent + = stap;

if(right(& n)!= Empty)

os<< *对(& n);

os<< setw(indent-stap)<< "" << el(& n)<< " BAL = QUOT; << bal(& n)<< endl;

if(left(& n)!= Empty)

os<< * left(& n);

indent - = stap;

return os;

}


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

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


class AVLtree

{

树树;

公共:

AVLtree():树(空){}

~AVLtree(){if(tree)delete tree; }

朋友ostream&运算符<< (ostream& os,AVLtree& t);

void Insert(T v){insert(tree,v); }

bool包含(T v){return contains(tree,v); }

bool Sorted(){return sorted(tree); }

bool Balanced(){int d = 0;回归平衡(树,d); }

};


ostream&运算符<< (ostream& os,AVLtree& t)

{

if(t.tree!= Empty)

os<< *(t.tree);

返回操作系统;

}

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


void main()

{

// int val = 2;

//节点< int> ; * ptr = new Node< int>(val);

// cout<< * ptr;


/ *节点< int> * t =空;

int rij [10] = {0,9,1,2,3 ,4,5,6,7,8};

for(int i = 0; i< 10; i + = 1)

{

insert(t,rij [i]);

cout<< * t<<结束;

}

* /


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 [i]);

cout<< t<< endl;

if(!t.Sorted()||!t.Balanced())

cout<< 不正确的AVL树\ n;

}

for(int j = -1; j< 11; j + = 1)

cout<< 包含 << j<< " = << boolalpha<< t。包含(j)<< endl;

}


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 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.


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::ostream& operator<<(std::ostream&, AVLtree&)".
Any advice?



Post the real code.

--
Regards,
Buster.


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[i]=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[i])
delete children[i];
}

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

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[i]);
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[i]);
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;
}


这篇关于C ++好友功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆