从一个不同标题中定义的类向一个函数授予友谊 [英] Granting friendship to a function from a class defined in a different header

查看:146
本文介绍了从一个不同标题中定义的类向一个函数授予友谊的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先这不是家庭作业,它的一个问题,从C ++第1卷,第5章ex 5的思考。
我需要做3个类,第一个授予友谊的内部的整个秒类和友谊只有一个来自第三类的函数。



我没有一个问题给予友谊的整个第二类,但授予第三类函数,如果我声明第三个类在同一个头theres没有问题。但在不同的头,我得到一些未定义的类型/声明。感谢您的帮助,这里是代码:

  #ifndef FIRSTCLASS_H 
#define FIRSTCLASS_H

// firstclasss头文件

#includesecondclass.h
#includethirdclass.h

class secondclass; //虚拟声明,所以它可以共享友谊
class thirdclass; //它不工作,当我想给一个函数友谊

class firstclass {
private:
int a;
int b;
public:
friend secondclass; //给整个类赋予友谊
friend void thirdclass :: z(firstclass *); // error
//使用未定义的类型'thirdclass'
//查看'thirdclass'的声明

};

#endif FIRSTCLASS_H



#ifndef THIRDCLASS_H
#define THIRDCLASS_H

// thirdclass头文件

#includefirstclass.h

class firstclass;

class thirdclass {
public:
void z(firstclass *);
};

#endif THIRDCLASS_H


解决方案

只有当您不包括相应类的标题时,您才需要提供转发声明。由于您已经包括 secondclass.h thirdclass.h ,您应该跳过相应的前向声明。 / p>

然而,在 thirdclass.h 中,不需要 firstclass.h :你正在声明一个指向 firstclass 的指针,而不是使用它的成员,所以你不需要include。



一般的规则是,如果你需要的是一个指针,你应该前进声明你的类,并且当你需要类的成员的知识时,包括他们的头。


First of all this is not 'homework', its a problem from Thinking in C++ Vol 1, chapter 5 ex 5. I need to make 3 classes, the first granting friendship of its internals to the whole second class, and friendship to only a function from the third class.

I dont have a problem with granting friendship to the entire 2nd class, but with granting to the third class function, if i declare the third class in the same header theres no problem. But in different headers i get some undefined type / declaration. Thanks for your help and here is the code:

#ifndef FIRSTCLASS_H
#define FIRSTCLASS_H

//firstclasss header file

#include "secondclass.h"
#include "thirdclass.h"

class secondclass; //dummy declaration so it can share friendship
class thirdclass;  //it doesnt work when i want to give friendship to a function

class firstclass{
private:
    int a;
    int b;
public:
    friend secondclass; //granting friendship to the whole class
    friend void thirdclass::z(firstclass *); //error
    //use of undefined type 'thirdclass'
    //see declaration of 'thirdclass'

};

#endif FIRSTCLASS_H



#ifndef THIRDCLASS_H
#define THIRDCLASS_H

//thirdclass header file

#include "firstclass.h"

class firstclass;

class thirdclass{
public:
    void z(firstclass *);
};

#endif THIRDCLASS_H

解决方案

You need to provide forward declarations only when you are not including the header for the corresponding class. Since you are including both secondclass.h and thirdclass.h already, you should skip the corresponding forward declarations altogether.

In the thirdclass.h, however, you do not need firstclass.h: you are declaring a pointer to firstclass, not using its members, so you do not need an include.

The general rule is that you should forward-declare your classes if all you need is a pointer, and include their headers when you need knowledge of the members of the class.

这篇关于从一个不同标题中定义的类向一个函数授予友谊的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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