Vc++ 6.0 如何避免重复包含一个头

有下面的自定义结构体,定义在sample.h中。

--------------------------------------------

typedef struct sample{

 int trueNumber;

 double feature[13];

}SAMPLE;

--------------------------------------------

 

A,类B#include<sample.h>,主程序都调用了类A,类B;就会出现

 

error C2011: ''sample'' : ''struct'' type redefinition

 

解决方法:写上宏定义:

 

-----------------------------------

#ifndef sample_H_H

#define  sample_H_H

   typedef struct sample{

   int trueClass;

   double feature[13];

  }SAMPLE;

#endif

---------------------------------------

 

也可以这样写

 

----------------------------------

#if !define sample_H_H

#define sample_H_H

   typedef struct sample{

   int trueClass;

   double feature[13];

  }SAMPLE;

#endif

-------------------------------

 

意思是:

 

 

if(sample_H_H,没有被定义过)

{

      定义宏sample_H_H

      ........(执行)other code

}

实际上sample_H_H作为一个标记而存在

 

 

自定义一个类时,在所有的头文件中都应用这组宏处理

注意是#ifndef不是#ifdef

 

-----------------------------------

#ifndef 标记名(常以类名_H_H,两个标记名要相同)

#define  标记名

 

//你原来的文件内容

 

#endif

---------------------------------

 

文章出处:飞诺网(www.firnow.com):http://dev.firnow.com/course/3_program/vc/vc_js/20100630/263391.html

原文地址:https://www.cnblogs.com/cy163/p/1930956.html