包括相关文件 [英] Including related files
问题描述
我感觉这是一个非常愚蠢的问题,我很抱歉,如果它是
问了很多。
想象一下,我有两个班级cShip和cPassenger。它们各自在自己的头文件cShip.h和cPassenger.h中都有一个
的定义,以及它们在cShip.cpp和cPassenger.cpp中的
实现。现在这里是每个标题
文件:
#ifndef CSHIP_H
#define CSHIP_H
>
class cShip
{
...
cPassenger * owner;
...
};
#endif
------下一个文件
#ifndef CPASSENGER_H
#define CPASSENGER_H
class cPassenger
{
...
cShip * current_vehicle;
...
};
# endif
现在我遇到的问题是如何将这些文件与包含
一起加入这些文件以便编译。 (这里的想法是船只总是有一个
的所有者,但乘客可以在他们不拥有的船上)。
很抱歉,如果这是一个非常平凡的问题,
Connell
Hi,
I have what feels like a really stupid question and I''m sorry if it is
asked a lot.
Imagine I have two classes cShip and cPassenger. They each have a
definition in their own header cShip.h and cPassenger.h and their
implementation in cShip.cpp and cPassenger.cpp. Now here is the header
file for each:
#ifndef CSHIP_H
#define CSHIP_H
class cShip
{
...
cPassenger * owner;
...
};
#endif
------Next file
#ifndef CPASSENGER_H
#define CPASSENGER_H
class cPassenger
{
...
cShip * current_vehicle;
...
};
#endif
Now the problem I have is how to join these files together with includes
such that they compile. (The idea here is that a ship always has an
owner but that a passenger can be in a ship that they don''t own).
Sorry if this is a really mundane question,
Connell
推荐答案
Connell Gauld写道:
Connell Gauld wrote:
我感觉这是一个非常愚蠢的问题而且我很抱歉,如果它被问了很多。
[...循环依赖问题已被删除...]
I have what feels like a really stupid question and I''m sorry if it is
asked a lot.
[...circular dependency question snipped...]
>
不要抱歉,只需在发布前阅读常见问题解答。
你可以在这里找到它: http://www.parashift.com/c++-faq-lite/
V
Don''t be sorry, just read the FAQ before posting.
You can find it here: http://www.parashift.com/c++-faq-lite/
V
所有者可能不是乘客,因此拥有一个单独的cOwner和cPassenger类可能更有意义。但要获得循环引用
编译,你可以简单地使用前向声明:
#define CSHIP_H
class cPassenger;
class cShip
{
...
cPassenger *老板;
...
};
#endif
#ifndef CPASSENGER_H
#define CPASSENGER_H
class cShip;
class cPassenger
{
...
cShip * current_vehicle;
...
};
#endif
这可能会给你带来一些问题,但是如果班级的任何部分都是
声明依赖于对前方宣布的
级的更深入了解。前向声明仅为您提供类的名称,而不是
有关数据成员或成员函数的任何详细信息。
An owner may not be a passenger, so it may make more sense to have a
separate cOwner and cPassenger class. But to get circular references to
compile, you can simply use a forward declaration:
#ifndef CSHIP_H
#define CSHIP_H
class cPassenger;
class cShip
{
...
cPassenger * owner;
...
};
#endif
#ifndef CPASSENGER_H
#define CPASSENGER_H
class cShip;
class cPassenger
{
...
cShip * current_vehicle;
...
};
#endif
This could cause you some problems though if any part of the class
declaration relies on deeper knowledge about the forward declared
class. A forward declaration only gives you the name of the class, not
any details about data members or member functions.
* Connell Gauld:
* Connell Gauld:
我感觉这是一个非常愚蠢的问题,如果它被问了很多话我很抱歉。
完全没有,这是一个很好的问题。
有许多非常相似的问题都涉及到
循环依赖。
在你的情况下,通常的解决方案,前向声明,是_not_
合适。
想象一下,我有两个班级cShip和cPassenger。它们各自在自己的头文件cShip.h和cPassenger.h中有一个
定义,并在cShip.cpp和cPassenger.cpp中实现它们的实现。现在这里是每个的标题
文件:
#ifndef CSHIP_H
#define CSHIP_H
类cShip
{
......
cPassenger *老板;
...
};
#endif
------下一个文件
#ifndef CPASSENGER_H
#define CPASSENGER_H
班级cPassenger
{
...
cShip * current_vehicle;
...
};
#endif
现在我遇到的问题是如何加入这些文件并包含
这样他们编译。 (这里的想法是,一艘船总是有一个
所有者,但乘客可以在他们不拥有的船上。)
I have what feels like a really stupid question and I''m sorry if it is
asked a lot.
Not at all, it''s a good question.
There are a host of very _similar_ questions that all involve
circular dependencies.
In your case the usual solution, forward declarations, is _not_
appropriate.
Imagine I have two classes cShip and cPassenger. They each have a
definition in their own header cShip.h and cPassenger.h and their
implementation in cShip.cpp and cPassenger.cpp. Now here is the header
file for each:
#ifndef CSHIP_H
#define CSHIP_H
class cShip
{
...
cPassenger * owner;
...
};
#endif
------Next file
#ifndef CPASSENGER_H
#define CPASSENGER_H
class cPassenger
{
...
cShip * current_vehicle;
...
};
#endif
Now the problem I have is how to join these files together with includes
such that they compile. (The idea here is that a ship always has an
owner but that a passenger can be in a ship that they don''t own).
你所拥有的是一个设计问题,而不是(只是)一个技术循环
依赖问题。
让船主成为cPerson。 />
让cPassenger成为一个来自cPerson的课程,呃,问题解决了;
然后你有
cShip取决于在CPerson上
cPassenger取决于cShip和cPerson
稍后您可能会考虑所有者可能是个人的情况_or_
a corporation。
随着这种扭曲变得更有趣...... ;-)
-
答:因为它弄乱了人们通常阅读文字的顺序。
问:为什么这么糟糕?
A:热门发布。
问:usenet和电子邮件中最烦人的事情是什么?
What you have is a design problem, not (just) a technical circular
dependency problem.
Let the owner of a ship be a cPerson.
Let cPassenger be a class derived from cPerson, and voilà, problem solved;
you then have
cShip depends on CPerson
cPassenger depends on cShip and cPerson
Later on you might consider the case where the owner might be a person _or_
a corporation.
With that twist it gets more interesting... ;-)
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
这篇关于包括相关文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!