问题描述
呀问题的话题已经讨论了很多次。而我对差异几乎是清楚的。我已经只是一个在书中涉及的例子疑问的。
Yeah the question topic has been discussed so many times. And I'm almost clear about the difference. I've just one doubt related to an example in the book.
此问题是,我在那里presented取2班为例相关书中C ++入门。
This question is related to my previous question, where I presented 2 classes taken as example in the book C++ Primer.
在参考这些类,书中引用了以下段落,特别是涉及到窗口管理器
类作为友元函数的成员函数的声明。下面是这样说的:
In reference to those classes, the book quotes the following paragraph, specially related to the declaration of member function of WindowManager
class as friend function. Here's what it says:
制作一个成员函数的朋友要求我们的节目仔细结构
适应声明和定义之间的相互依存关系。在这
例如,我们必须按如下顺序我们的程序:
- 首先,定义Window_mgr类,它声明,但无法定义,明确的。
屏幕前必须明确的声明可以使用屏幕上的成员。 - 接下来,定义级屏幕,其中包括一个朋友声明清楚。
- 最后,定义清晰,它现在可以参照成员画面。
在这个问题psented的code我$ P $如下仅此结构。但似乎它不工作了。这使我认为,如果上述各点的误导还是我没有正确地执行它。
The code I presented in that question follows this structure only. But it seems that it is not working out. This makes me think if the above points are misguiding or I didn't implement it correctly.
问题是,当我宣布明确
功能在 ScreenCls
友元函数类,我陷入循环列入的头文件。我在这里再次外判两个类的特定部分:
The problem is, when I declare the clear
function as friend function in ScreenCls
class, I fall into cyclic inclusion of header files. I'll brief out the specific part of both classes here again:
#ifndef SCREENCLS_H
#define SCREENCLS_H
#include <iostream>
#include "WindowManager.h"
using namespace std;
class ScreenCls {
friend void WindowManager::clear(ScreenIndex);
// Some other code
}
在这里,我以包括 WindowManager.h
头文件,如明确
函数现在使用 ScreenIndex
那里定义。 转发宣言的不会在这里(纠正我,如果我错了)。
Here I've to include the WindowManager.h
header file, as clear
function is now using the ScreenIndex
defined there. Forward Declaration won't work here (Correct me if I'm wrong).
现在,接下来我们进入到 WindowManager.h
:
Now, next we move on to WindowManager.h
:
#ifndef WINDOWMANAGER_H
#define WINDOWMANAGER_H
#include <iostream>
#include <vector>
#include "ScreenCls.h"
using namespace std;
class WindowManager {
public:
// location ID for each screen on window
using ScreenIndex = vector<ScreenCls>::size_type;
private:
vector<ScreenCls> screens{ ScreenCls(24, 80, ' ') };
};
和精力集中到屏幕的声明
在这里。他们使用列表中初始化的添加默认的 ScreenCls
到矢量
。所以,在这里我们再次需要包括 WindowManager.h
。现在我们进入循环包容。这$ P $从建设pvents我的项目。
And concentrate onto the declaration of screens
here. They have used list initializer to add a default ScreenCls
to the vector
. So, here again we need to include the WindowManager.h
. And now we are into cyclic inclusion. This prevents my project from building.
不过,如果我改变友元函数声明,使整个类的朋友,那么我可以用工作向前声明
的窗口管理器
类。在这种情况下,它会正常工作。
However, if I change the friend function declaration to make the entire class as friend, then I can work with forward declaring
the WindowManager
class. In that case, it will work fine.
所以,基本上友元函数不能在这里工作,但友元类工作。那么,是不是说以上几点都不能与实施进展顺利,或有什么毛病我的班?我只是想知道这清楚地了解头包容的概念
和向前声明
。
So, basically friend function isn't working here, but friend class is working. So, is it that the above points are not going well with their implementation, or there's something wrong with my classes? I just want to know this to clearly understand the concept of header inclusion
and forward declaration
.
在我的previous问题描述以及链接的问题。但是,这只是它不是在上述情况下工作,所以我再次要求它。
The questions linked in my previous question describe that well. But it's just that it's not working in the above situation, so I'm asking it again.
推荐答案
我猜你的问题是与屏幕初始化。你不能初始化是一个类中* .h文件的任何数据。所以,我建议你做这样的事情:
I guess your problem is with the screen initializer. You cannot initialize any data in *.h files that are inside a class. So, I suggest you to do something like that:
#ifndef WINDOWMANAGER_H
#define WINDOWMANAGER_H
#include <iostream>
#include <vector>
//#include "ScreenCls.h"
using namespace std;
class ScreenCls;
class WindowManager {
public:
// location ID for each screen on window
using ScreenIndex = vector<ScreenCls>::size_type;
private:
vector<ScreenCls> screens; //{ ScreenCls(24, 80, ' ') }; remove this
};
这篇关于友元类或朋友的成员函数 - 转发声明并列入头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!