C语言 如何在 C++ 中创建我自己的头文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20015656/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to create my own header file in c++?
提问by Jefree Sujit
I m a new budding programmer.Is it possible for me to create a new header file of my own? Can anyone help me how to create my own header file in c++ with an example ?
我是初出茅庐的程序员。我可以创建自己的新头文件吗?任何人都可以通过示例帮助我如何在 C++ 中创建我自己的头文件吗?
回答by Jefree Sujit
Yes, of course you can, but before that you need to learn what header files are and how you can use them properly.
是的,当然可以,但在此之前,您需要了解什么是头文件以及如何正确使用它们。
file: yourname.h
文件:你的名字.h
#ifndef YOUR_NAME_INCLUDE
#define YOUR_NAME_INCLUDE
/* Your function statement here */
#endif
yourname.cpp
你的名字.cpp
#include <iostream.h>
#include "yourname.h"
/* Your function definition here */
main.cpp
主程序
#include <iostream.h>
#include "yourname.h"
/* Your function calling here */
To learn more about header files and include statements, click the link below.
要了解有关头文件和包含语句的更多信息,请单击下面的链接。
回答by Ashutosh
Yes, you can create your own header file.
是的,您可以创建自己的头文件。
- Kindly go through thinking in c++ by bruce eckel vol 1.
- Just to begin with, a header file is which has extension '.h'/'.hpp'
- These files have declaration of user defined data structures and interfaces such has class declaration, function prototypes and etc.
- After declaring and storing it into project folder. you need to include this file in .cpp/.c eg.:
- 请通过 bruce eckel 第 1 卷在 C++ 中思考。
- 首先,头文件的扩展名为“.h”/“.hpp”
- 这些文件具有用户定义的数据结构和接口的声明,例如类声明、函数原型等。
- 声明并存储到项目文件夹后。您需要将此文件包含在 .cpp/.c 中,例如:
file: myheader.h
文件:myheader.h
#ifndef MYHEADER
#define MYHEADER
......
#endif
file: myclass.cpp
文件:myclass.cpp
#include <iostream.h>
#include "myheader.h"

