递归的朋友类 [英] recursive friend classes

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

问题描述

有任何方法:

  B类; 

class C {
public:
C(){}
private:
int i;
friend B :: B();
};

class B {
public:
B(){}
private:
int i;
friend C :: C();
};

产生错误:

  prog.cpp:8:错误:无效使用不完整的类型'struct B'
prog.cpp:1:错误:正向声明'struct B'


解决方案

你不能这样做。删除循环依赖项。


Is there any way around this:

class B;

class C { 
 public:
  C() { }
 private:
  int i;
  friend B::B();
};

class B { 
 public:
  B() { }
 private:
  int i;
  friend C::C();
};

Gives error:

prog.cpp:8: error: invalid use of incomplete type ‘struct B’
prog.cpp:1: error: forward declaration of ‘struct B’

解决方案

You just can't do this. Remove the circular dependency.

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

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