1. 程式人生 > > String標頭檔案與CString標頭檔案

String標頭檔案與CString標頭檔案

首先說string的標頭檔案

1、<string>

包裝了std的C++標頭檔案

2、<string.h>

舊的C標頭檔案

3、<cstring.h>

舊C標頭檔案的std版本,切記,這不是cstring的標頭檔案

詳見effective c++ 的第49條

再說cstring的標頭檔案

注:VC9編譯環境下

1、<atlstr.h>

非MFC版本,控制檯程式就用這個

2、<afxstr.h>

MFC版本,需要連結MFC的dll或靜態庫。網上很多人說要包含<afx.h>,<afx.h>包含的東西就比較多了CObject及其派生類,還有檔案類、時間類、異常類、字串類等等(700多行的位置包含了afxstr.h),如果僅僅需要cstring的話,包含<afxstr.h>就夠了。

<afx.h>中的預處理

#ifdef _DLL

#ifndef _AFXDLL

#error Building MFC application with /MD[d] (CRT dll version) requires MFC shared dll version. Please #define _AFXDLL or do not use /MD[d]

#endif

#endif

<afxstr.h>中的預處理

#ifndef _AFX

#error afxstr.h can only be used in MFC projects.  Use atlstr.h

#endif

注意上面的#error,大意就是這個標頭檔案是在MFC工程裡用的。要麼改用<atlstr.h>,要麼在專案->屬性->常規裡設定為在共享的dll中使用MFC。