松鼠的博客

导航

Vc++ 6.0 如何避免重复包含一个头文件 error C2011:

有下面的自定义结构体,定义在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
---------------------------------

posted on 2009-06-24 13:38  Xproer-松鼠  阅读(322)  评论(0)    收藏  举报