我在使用VS 2013 编写一个头文件代码内容如下:
1 // 这个文件的名称为Common.h 2 typedef struct tagMSGINFO //传输消息结构体 3 { 4 // int messageType; 5 int messageID; 6 BYTE transData[1024 * 5]; 7 }MSGINFO_SERVER; 8 9 typedef struct tagMSGTYPE 10 { 11 int messageType; 12 }MSGTYPE_SERVER; 13 14 typedef struct tagSYSTEMINFO 15 { 16 int os; 17 bool Cam; //摄像头 18 double ver; 19 }SYSTEMINFO_SERVER; 20 21 typedef struct tagDRIVER 22 { 23 wchar_t disk; 24 double all; 25 double free; 26 int type; 27 }DRIVER; 28 29 typedef struct tagFILEINFO 30 { 31 TCHAR FileName[256]; 32 int type; 33 __int64 size; 34 }FILEINFO; 35 36 typedef struct tagDOWNFILEDATA 37 { 38 BYTE context[512]; // 每块的大小 39 UINT size; // 从文件的那个位置开始传输 40 bool flag; // 是否传输完毕的标志 41 UINT count; // 进度条要用到 42 }DOWNFILEDATA; 43 44 typedef struct tagCMD //CMD命令信息 45 { 46 int flag; 47 char command[1024]; 48 }CMD; 49 50 typedef struct tagTASK 51 { 52 wchar_t TaskName[260]; 53 DWORD TaskID; 54 wchar_t TaskPath[260]; 55 bool flag; 56 }TASK; 57 58 59 typedef struct tagBMPDATA 60 { 61 BITMAPINFO bmpheadinfo; 62 int Id; // 0--图像信息头 1--图像数据 63 bool Show; // 是否可以显示图像 64 int Size; 65 int HeadSize; 66 UINT Begin; 67 BYTE Data[5000]; 68 }BMPDATA;
编译时出现了如下的错误:
1 e:\vctest\romatecontrolserver2\romatecontrolserver2\comment.h(20): error C2011: “tagMSGINFO”:“struct”类型重定义 2 1> e:\vctest\romatecontrolserver2\romatecontrolserver2\comment.h(20) : 参见“tagMSGINFO”的声明 3 1>e:\vctest\romatecontrolserver2\romatecontrolserver2\comment.h(27): error C2011: “tagMSGTYPE”:“struct”类型重定义 4 1> e:\vctest\romatecontrolserver2\romatecontrolserver2\comment.h(27) : 参见“tagMSGTYPE”的声明 5 1>e:\vctest\romatecontrolserver2\romatecontrolserver2\comment.h(32): error C2011: “tagSYSTEMINFO”:“struct”类型重定义 6 1> e:\vctest\romatecontrolserver2\romatecontrolserver2\comment.h(32) : 参见“tagSYSTEMINFO”的声明 7 1>e:\vctest\romatecontrolserver2\romatecontrolserver2\comment.h(39): error C2011: “tagDRIVER”:“struct”类型重定义 8 1> e:\vctest\romatecontrolserver2\romatecontrolserver2\comment.h(39) : 参见“tagDRIVER”的声明 9 1>e:\vctest\romatecontrolserver2\romatecontrolserver2\comment.h(47): error C2011: “tagFILEINFO”:“struct”类型重定义 10 1> e:\vctest\romatecontrolserver2\romatecontrolserver2\comment.h(47) : 参见“tagFILEINFO”的声明 11 1>e:\vctest\romatecontrolserver2\romatecontrolserver2\comment.h(54): error C2011: “tagDOWNFILEDATA”:“struct”类型重定义 12 1> e:\vctest\romatecontrolserver2\romatecontrolserver2\comment.h(54) : 参见“tagDOWNFILEDATA”的声明 13 1>e:\vctest\romatecontrolserver2\romatecontrolserver2\comment.h(62): error C2011: “tagCMD”:“struct”类型重定义 14 1> e:\vctest\romatecontrolserver2\romatecontrolserver2\comment.h(62) : 参见“tagCMD”的声明 15 1>e:\vctest\romatecontrolserver2\romatecontrolserver2\comment.h(68): error C2011: “tagTASK”:“struct”类型重定义 16 1> e:\vctest\romatecontrolserver2\romatecontrolserver2\comment.h(68) : 参见“tagTASK”的声明 17 1>e:\vctest\romatecontrolserver2\romatecontrolserver2\comment.h(81): error C2011: “tagBMPDATA”:“struct”类型重定义 18 1> e:\vctest\romatecontrolserver2\romatecontrolserver2\comment.h(81) : 参见“tagBMPDATA”的声明
出错的原因是因为你在多个类中引用了Common.h 这个文件,例如你在类A.h 中使用代码:
1 #include "Common.h"
而你在类B.h 中同样使用了代码:
1 #include "Common.h"
这样VS 就会对“Common.h” 这个文件进行再次编译,这样就会出现本文上边提到的问题,解决的办法很简单,只要在“Common.h” 这个文件的第一行添加如下代码:
1 #pragma once
这个代码的意思是对该文件只会进行一次编译。
2015-02-14 17:20:06