我一直在阅读关于标头警卫和他们的用来解决重定义错误,但我不太清楚如何正确实施它。这里是什么,我试图做一个简单的例子:基本的C++重定义错误
fileA.h
#include first_header.h
#include second_header.h
// code here
fileB.h
#include first_header.h
#include second_header.h
// code here
mainfile.cpp
#include fileA.h
#include fileB.h
// code here
现在问题出现在mainfile.cpp中,因为我需要包含fileA.h和fileB.h头文件,但是每个头文件都包含相同的头文件引用,因此给我带来了重定义错误。在这种情况下,我不太确定如何绕过它或正确实施标头警卫。
如果未定义,可以使用#ifndef等预处理程序语句。 –
如果'first_header.h'和'second_header.h'包含警卫,那么应该没有问题。在'mainfile.cpp'中,编译器将打开'fileB.h',然后打开'first_header.h',意识到包含头定义被定义并忽略了文件中的所有内容。 –
请提供[mcve]。你没有显示first_header.h和second_header.h,也没有显示他们有(或没有)他们自己的看守。 –