我在使用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
