头文件 [英] header file

查看:78
本文介绍了头文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用C ++为购物中心项目创建头文件?

How can i make a header file for a shopping mall project in C++?

推荐答案

您应该首先返回教程并学习C ++.这里没有人会为您编写程序,因此请提出具体问题.例如,您到底是什么问题,您究竟尝试了什么以及为何不起作用,...
You should first go back to your tutorials and learn C++. Nobody here will write the program for you, so ask specific questions. For instance, what is your problem exactly, what did you try yet and why it didn''t work, ...


C ++标头的一般结构是

从Macro Guard开始.
这可以是#pragma once(仅Microsoft Visual Studio 7.1或更高版本)或#define
如果头文件来自多个位置,则这将停止多次定义任何类,函数和外部变量.
如果使用#define,则定义的变量对于整个解决方案必须是唯一的,常见的命名方式是__HEADERFILENAME_H__,其中HEADERFILENAME是.h文件的名称

然后,您只需为类,结构,枚举,...的功能和定义提供原型....

MyHeader.h
The general structure for C++ headers is

Start with a Macro Guard.
This can either be a #pragma once (Microsoft Visual Studio 7.1 or later only) or a #define
This stops any classes, functions and external variables getting defined multiple times if the header is included from multiple locations.
If you use the #define the variable defined must be unique for the entire solution, a common naming style is __HEADERFILENAME_H__ where HEADERFILENAME is the name of the .h file

Then you simply provide prototypes for the functions and definitions of classes, structs, enums, ...

MyHeader.h
#ifndef __MYHEADER_H__ //Must be unique
	#define __MYHEADER_H__ //Must match the previous line

	typedef struct {
		int nSomeVar;
	} SomeStruct;

	int SomeFunction();
#endif



想法是int SomeFunction();的代码与您要使用的int SomeFunction();文件位于不同的.cpp文件中.



The idea is that the code for int SomeFunction(); is in a different .cpp file than what you are trying to use it from


您不会为项目"创建头文件(无论您说的是什么,这都是一个重载的术语),您都用它来描述与某物的接口.想法是,头文件包含在独立且完整的情况下使用某些内容所需的最少信息量.

该界面可以是:

-在这些函数的接口中使用的一组函数和关联的数据类型(例如,旧的C样式stdio)

-类定义(例如C ++ basic_string类)

-一组类声明,用于通过不需要知道它们是什么的大量代码来传递不完整或未定义的对象(例如,它们存在)(例如C ++标准库iosfwd)

-还有其他一些功能,包括宏扩展(用于生成程序)和显式模板实例化(用于从模板类为特定的参数化类型创建库),但是如果您不知道头文件是什么,我就不必担心那个.

顺便说一句,您不一定总是需要其他张贴者提到的包含保护程序(#ifndef FOO _/#define FOO _/#endif垃圾).仅包含非定义声明或函数原型的文件不需要它们.尽管使用它们可能是一个好习惯,但最好了解您在哪里以及为什么需要它们.

无论如何,要全面了解包含文件以及如何在C ++中使用它们,请自己购买Bjarne Stroustrup编写的"The C ++ Programming Language"副本(或从库中获取一个).

干杯,

You don''t make a header file for "a project" (whatever you mean by one of those, it''s an overloaded term) you make one to describe an interface to something. The idea is that a header file contains the minimum amount of information required to use something while being standalone and complete.

This interface could be:

- a set of functions and associated data types used in the interfaces to those functions (e.g. old C style stdio)

- a class definition (e.g. the C++ basic_string class)

- a set of class declarations used to pass incomplete or undefined objects through lumps of code that don''t need to know what they are, just that they exist (e.g. the C++ standard library iosfwd)

- and a few more including macro expansions (for generative programming) and explicit template instantiations (for creating libraries from template classes for specific parameterising types), but if you don''t know what a header file is I wouldn''t worry about that yet.

Incidentally, you don''t always need an include guard (the #ifndef FOO_/#define FOO_/#endif rubbish) other posters have mentioned. Files that only contain non-defining declarations or function prototypes don''t need them. Although it might be a good habit to get into using them it''s even better to understand where you need them and why.

Anyway, for the full lowdown on include files and how to use them in C++ go and buy yourself a copy of "The C++ Programming Language" by Bjarne Stroustrup (or get one from a library).

Cheers,

Ash


这篇关于头文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆