简体   繁体   English

3个.h和3个.cpp文件

[英]3 .h and 3 .cpp files

I'm building an application that should use one class from 1.h in 2.h it is defined like: 我正在构建应使用2.h中的1.h中的一个类的应用程序,其定义如下:

<classname> *p;

now i can use p in 2.cpp. 现在我可以在2.cpp中使用p了。 But I would also like to use it in 3.cpp. 但我也想在3.cpp中使用它。 I could not include the 2.h in 3.cpp. 我无法在3.cpp中包括2.h。 Is there a way to make it visible in 3.cpp? 有没有办法使它在3.cpp中可见?

Thanks for the help. 谢谢您的帮助。

A more descriptive example of your problem with some code would be valuable. 关于某些代码问题的更具描述性的示例将很有价值。

In general attempt to do the following: 通常,尝试执行以下操作:

  • Each header should contain only the its own class with the same name. 每个标头应仅包含其自己的具有相同名称的类。
  • If a class is using another class, and you can forwardly declare it in that header, do. 如果一个类正在使用另一个类,并且您可以在该标头中向前声明它,请执行。

You cannot forwardly declare if the class has an instance of another class or if it derives from it. 您不能向前声明该类是否具有另一个类的实例,或者它是否派生自该类。

  • Compilation units (cpp) files should include all the headers they need. 编译单元(cpp)文件应包含所需的所有标头。 Ideally include its own header first. 理想情况下,首先应包含其自己的标题。

You need to provide more info but I think I got it Your trying to use a class in a header that was defined I. A another header, but for some reason you can't include the header it was defined into the header your working on. 您需要提供更多信息,但我想我明白了。您尝试在已定义的标头中使用一个类。另一个标头,但由于某种原因,您无法将其定义的标头包含在正在使用的标头中。 The solution is to create a "temporary" definition instead of an include. 解决方案是创建“临时”定义而不是包含。

Eg: U need myClassA in 1.h but it was defined in 2.h and some error doesn't let you include 2.h into 1.h to solve it you.simply Write: 例如:您需要在1.h中使用myClassA,但是它是在2.h中定义的,并且有些错误不允许您将2.h包含到1.h中来解决它。

class myClassA; 类myClassA;

on top of 1.h so you can reference it, but no implementation (because it is implemented in 2.cpp) 在1.h之上,因此您可以引用它,但是没有实现(因为它是在2.cpp中实现的)

A simple rule is one class, one header file. 一个简单的规则是一类,一个头文件。

Given: 鉴于:

A.h:
class A
{
};

B.h:
#include "A.h"
class B
{
  A obj;
};

C.h:
#include "A.h"
class C
{
  A obj;
};

I hope this answers the question. 我希望这能回答这个问题。 Otherwise, edit your question with more details and example of what you are trying to accomplish. 否则,请使用更多详细信息和要完成的示例来编辑您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM