有什么办法可以声明两个类的共同朋友功能吗? [英] Is there any way to declare mutual friend functions for two classes

查看:70
本文介绍了有什么办法可以声明两个类的共同朋友功能吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

class CDB;

class CDM
{
public:
    friend CDB& CDB::Add(const CDM&);
    CDM& Add(const CDB&);
};

class CDB
{
public:
    CDB& Add(const CDM&);
    friend CDM& CDM::Add(const CDB&);
};

这段代码给我错误:错误C2027:使用未定义类型'CDB'.如何解决这个问题.

This code gives me the error : error C2027: use of undefined type 'CDB'. How to resolve this.

推荐答案

不,您不能这样做.无法消除循环依赖.

No, you can't do that. There is no way to remove the cyclic dependency.

您应该能够使类 CDB 成为 CDM 的朋友,而不是希望使 CDB :: Add()朋友.

You should be able to get by with making the class CDB a friend of CDM instead of wanting to making CDB::Add() a friend.

class CDB;

class CDM
{
   public:
      friend class CDB;
      CDM& Add(const CDB&);
};

class CDB
{
   public:
      CDB& Add(const CDM&);
      friend CDM& CDM::Add(const CDB&);
};

这篇关于有什么办法可以声明两个类的共同朋友功能吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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