在 VC++ 6.0 中使用 std::min 和 std::max 時,編譯器報錯:
error C2039: 'min' : is not a member of 'std'
error C2039: 'max' : is not a member of 'std'
程式碼範例:
center = std::min(std::max(0.0, center), 1.0);
<algorithm> 中可能缺少 min/max 模板,或未置於 std 命名空間。<windows.h> 定義了 min/max 宏,若在包含標準頭文件前包含它,會導致標準函式被宏替換而產生語法錯誤。::min 或使用 using namespace std;。NOMINMAX 宏(最推薦)在包含 <windows.h> 之前定義此宏,防止宏定義汙染:
#define NOMINMAX
#include <windows.h>
#include <algorithm> // 確保包含此頭文件
// 現在可以安全使用 std::min/max
center = std::min(std::max(0.0, center), 1.0);
若已包含 windows.h 且無法修改順序,用括號屏蔽宏展開:
center = (std::min)((std::max)(0.0, center), 1.0);
// 簡易模板實現
template <typename T>
const T& my_min(const T& a, const T& b) { return (a < b) ? a : b; }
template <typename T>
const T& my_max(const T& a, const T& b) { return (a > b) ? a : b; }
// 或使用 Microsoft 專有函式(非標準)
// #include <stdlib.h> // for _cpp_max, _cpp_min (舊版)
// center = _cpp_min(_cpp_max(0.0, center), 1.0);
VC++ 6.0 已停止支援超過 20 年,強烈建議遷移至 Visual Studio 2019/2022,獲得完整 C++17/20 支援、更好的標準庫實現與安全性。
#define NOMINMAX。<algorithm>、<string> 等),再包含平台特定頭文件(<windows.h>)。std::min/max 時顯式指定模板參數避免歧義:std::min<double>(a, b)。using namespace std; 在全域範圍,防止名稱衝突。