什么是C ++中的头文件的保护块? [英] What is a guard block for a header file in C++?

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

问题描述

我正在尝试使用Code :: Blocks IDE创建C ++类,并且有一个名为保护块"的字段.我已经进行了搜索,但是找不到任何有用的信息.这个字段是做什么用的?谢谢.

I'm trying to make a C++ class using the Code::Blocks IDE and there is a field called "Guard block." I've done a search and haven't been able to find any useful information. What is this field for? Thanks.

推荐答案

保护块用于防止同一编译单元(c ++文件)多次包含头文件.他们看起来像这样:

Guard blocks are used to protect against the inclusion of a header file multiple times by the same compilation unit (c++ file). They look something like this:

// Foo.h
#ifndef INCLUDE_FILE_NAME_HERE_H_
#define INCLUDE_FILE_NAME_HERE_H_

class Foo
{
};


#endif

如果在同一个文件中包含多个文件,则最终将出现多个定义错误.在小型项目中不需要使用包含保护,但是在任何中型到大型项目中都变得至关重要.我通常在我编写的任何头文件中使用它.

If you include the same file multiple files, you will end up with multiple definition error. Using of include guards isn't necessary in small projects but becomes critical in any medium to large sized projects. I use it routinely on any header files I write.

这篇关于什么是C ++中的头文件的保护块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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