如何解決在 Windows 程序中使用了 C++ 標準庫文件 ( STL ) 后出現的問題?如何解決在 Windows 程序中使用了 C++ 標準庫文件 ( STL ) 后出現的問題?

熱心網友

在 Windows 的程序設計中,使用了標準庫 ( STL ) 后就產生了很多語法錯誤,可想而知它們兩者之間有沖突。原因就在于一方面 min 和 max 宏在 Windef。h 定義成: #ifndef NOMINMAX #ifndef max #define max(a,b) (((a) (b)) ? (a) : (b)) #endif #ifndef min #define min(a,b) (((a) inline const T& min(const T& a, const T& b) { return b inline const T& min(const T& a, const T& b, Compare comp) { return comp(b, a) ? b : a; } template inline const T& max(const T& a, const T& b) { return a inline const T& max(const T& a, const T& b, Compare comp) { return comp(a, b) ? b : a; } 已經在 Windef。h 中定義成了一個宏,同樣名稱的函數又在 STL 中被定義了,由此可知,當你同時包含了 Windef。h( 或者 Windows。h ) 和 Algobase。h ( 來自 STL ) 時,就會產生語法錯誤。所以,我們要屏蔽去掉其中的一個定義。 --------------------------------------------------------------- 下面的做法是用 NOMINMAX 來屏蔽掉 Windef。h 中的 min 和 max 。 在 Visual C++ 中使用菜單 Project - Settings 在 C/C++ tab 頁的 Preprocessor definitions 文本編輯框,把 NOMINMAX 添加到其中,然后使用菜單 Build-Rebuild All重新編譯所有文件。。