头文件的多个包含 [英] Multiple inclusions of header files

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

问题描述

我有 A.cpp B.cpp ,它们都包含头文件 header.h .

I have A.cpp and B.cpp which both include the header file header.h.

以后, A.cpp B.cpp 都包含在 main.cpp 中.

编译 main.cpp 时,会导致错误,指出头文件 header.h 已被包含两次.

This causes error when main.cpp is compiled saying that the header file header.h has been included twice.

如何解决这个问题?

推荐答案

您应将头文件放在

#ifndef MYFILE_H
#define MYFILE_H

// Contents of your file

#endif

这些被称为包含防护.

第二点:您不应只包含.cpp文件,而只包含.h文件.

Second point: you shouldn't be including .cpp files, only .h files.

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

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