首先,这不是“作业”,它是 Thinking in C++ Vol 1, Chapter 5 ex 5 中的一个问题。我需要制作 3 个类,第一个将其内部的友谊授予整个第二个班级,而友谊只授予一个第三类的功能。
我对整个第二类授予友谊没有问题,但是在授予第三类功能时,如果我在同一个标题中声明第三类,则没有问题。但是在不同的标题中,我得到了一些未定义的类型/声明。感谢您的帮助,这里是代码:
#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