C ++标头和实现文件:要包含什么?

时间:2023-01-13 09:54:48

There is a .h file and a .cpp file with the same name but different extension.

有一个.h文件和一个.cpp文件,名称相同但扩展名不同。

If I want to use what's in the .cpp file, do I include the .h file or the .cpp file?

如果我想使用.cpp文件中的内容,是否要包含.h文件或.cpp文件?

3 个解决方案

#1


22  

The simple answer is that you almost always want to include .h files, and compile .cpp files. CPP files are (usually) the true code, and H files are (usually) forward-declarations.

简单的答案是你几乎总是想要包含.h文件,并编译.cpp文件。 CPP文件(通常)是真正的代码,H文件是(通常)前向声明。

The longer answer is that you may be able to include either, and it might work for you, but both will give slightly different results.

更长的答案是你可以包含其中之一,它可能对你有用,但两者都会给出稍微不同的结果。

What "include" does is basically copy/paste the file in at that line. It doesn't matter what the extension is, it will include the contents of the file the same way.

“包含”的作用基本上是在该行复制/粘贴文件。扩展名是什么并不重要,它将以相同的方式包含文件的内容。

But C++ code is, by convention, usually written this way:

但按照惯例,C ++代码通常以这种方式编写:

SomeClass.cpp -

#include "SomeClass.h"
#include <iostream>

void SomeClass::SomeFunction()
{
  std::cout << "Hello world\n";
}

SomeClass.h -

class SomeClass
{
  public:
    void SomeFunction();
};

If you include either of those, you can use the code from it. However, if you have multiple files that include the same .cpp file, you may get errors about re-definition. Header files (.h files) usually contain only forward declarations, and no implementations, so including them in multiple places won't give you errors about re-definition.

如果包含其中任何一个,则可以使用其中的代码。但是,如果您有多个包含相同.cpp文件的文件,则可能会出现有关重新定义的错误。头文件(.h文件)通常只包含前向声明,没有实现,因此将它们包含在多个位置不会给您带来有关重新定义的错误。

If you somehow manage to compile without errors when including .cpp files from other .cpp files, you can still end up with duplicate code. This happens if you include the same .cpp files in multiple other files. It's like you wrote the function twice. This will make your program bigger on disk, take longer to compile, and run a bit slower.

如果你在包含来自其他.cpp文件的.cpp文件时以某种方式设法编译而没有错误,你仍然可以得到重复的代码。如果在多个其他文件中包含相同的.cpp文件,则会发生这种情况。这就像你写了两次函数。这将使您的程序在磁盘上更大,编译时间更长,运行速度更慢。

The main caveat is that this implementation/forward declaration convention doesn't hold true for code that uses templates. Template code will still be handed to you as .h files, but it (usually) is implemented directly in the .h file, and won't have accompanying .cpp files.

主要的警告是,对于使用模板的代码,此实现/转发声明约定不适用。模板代码仍将作为.h文件传递给您,但它(通常)直接在.h文件中实现,并且不会附带.cpp文件。

#2


3  

The .h file usually contains the class declaration and the .cpp file the class definition (implementation). You should include the .h in the .cpp file.

.h文件通常包含类声明,而.cpp文件包含类定义(实现)。您应该在.cpp文件中包含.h。

A good rule of thumb is to NEVER include a .cpp file, because the #include directive just copies the content of the included file into the including file. You may end with some multiple inclusion / definition and you definitely don't want to do that.

一个好的经验法则是永远不要包含.cpp文件,因为#include指令只是将包含文件的内容复制到包含文件中。您可能会以一些多重包含/定义结束,但您绝对不希望这样做。

#3


3  

Usually it's better to write in the header file .h

通常最好在头文件中写入.h

#ifndef H_someClass
#define H_someClass

class SomeClass { public: void SomeFunction(); };

class SomeClass {public:void SomeFunction(); };

#endif

that in order not to get error about re-definition when u need to include the .cpp file in other files.

当你需要在其他文件中包含.cpp文件时,为了不得重新定义错误。

#1


22  

The simple answer is that you almost always want to include .h files, and compile .cpp files. CPP files are (usually) the true code, and H files are (usually) forward-declarations.

简单的答案是你几乎总是想要包含.h文件,并编译.cpp文件。 CPP文件(通常)是真正的代码,H文件是(通常)前向声明。

The longer answer is that you may be able to include either, and it might work for you, but both will give slightly different results.

更长的答案是你可以包含其中之一,它可能对你有用,但两者都会给出稍微不同的结果。

What "include" does is basically copy/paste the file in at that line. It doesn't matter what the extension is, it will include the contents of the file the same way.

“包含”的作用基本上是在该行复制/粘贴文件。扩展名是什么并不重要,它将以相同的方式包含文件的内容。

But C++ code is, by convention, usually written this way:

但按照惯例,C ++代码通常以这种方式编写:

SomeClass.cpp -

#include "SomeClass.h"
#include <iostream>

void SomeClass::SomeFunction()
{
  std::cout << "Hello world\n";
}

SomeClass.h -

class SomeClass
{
  public:
    void SomeFunction();
};

If you include either of those, you can use the code from it. However, if you have multiple files that include the same .cpp file, you may get errors about re-definition. Header files (.h files) usually contain only forward declarations, and no implementations, so including them in multiple places won't give you errors about re-definition.

如果包含其中任何一个,则可以使用其中的代码。但是,如果您有多个包含相同.cpp文件的文件,则可能会出现有关重新定义的错误。头文件(.h文件)通常只包含前向声明,没有实现,因此将它们包含在多个位置不会给您带来有关重新定义的错误。

If you somehow manage to compile without errors when including .cpp files from other .cpp files, you can still end up with duplicate code. This happens if you include the same .cpp files in multiple other files. It's like you wrote the function twice. This will make your program bigger on disk, take longer to compile, and run a bit slower.

如果你在包含来自其他.cpp文件的.cpp文件时以某种方式设法编译而没有错误,你仍然可以得到重复的代码。如果在多个其他文件中包含相同的.cpp文件,则会发生这种情况。这就像你写了两次函数。这将使您的程序在磁盘上更大,编译时间更长,运行速度更慢。

The main caveat is that this implementation/forward declaration convention doesn't hold true for code that uses templates. Template code will still be handed to you as .h files, but it (usually) is implemented directly in the .h file, and won't have accompanying .cpp files.

主要的警告是,对于使用模板的代码,此实现/转发声明约定不适用。模板代码仍将作为.h文件传递给您,但它(通常)直接在.h文件中实现,并且不会附带.cpp文件。

#2


3  

The .h file usually contains the class declaration and the .cpp file the class definition (implementation). You should include the .h in the .cpp file.

.h文件通常包含类声明,而.cpp文件包含类定义(实现)。您应该在.cpp文件中包含.h。

A good rule of thumb is to NEVER include a .cpp file, because the #include directive just copies the content of the included file into the including file. You may end with some multiple inclusion / definition and you definitely don't want to do that.

一个好的经验法则是永远不要包含.cpp文件,因为#include指令只是将包含文件的内容复制到包含文件中。您可能会以一些多重包含/定义结束,但您绝对不希望这样做。

#3


3  

Usually it's better to write in the header file .h

通常最好在头文件中写入.h

#ifndef H_someClass
#define H_someClass

class SomeClass { public: void SomeFunction(); };

class SomeClass {public:void SomeFunction(); };

#endif

that in order not to get error about re-definition when u need to include the .cpp file in other files.

当你需要在其他文件中包含.cpp文件时,为了不得重新定义错误。