Visual C++에서 min, max 함수를 사용한 경우 C2589, C2059 컴파일 오류 발생
다음과 같이 간단한 코드를 추가했는데,
int max_1 = std::numeric_limits<int>::max();
C2589, C2059 컴파일 오류가 발생했습니다.
Error C2589 '(': illegal token on right side of '::')
Error C2059 syntax error: '::'
분명히 #include <limits>까지 되어 있는데도 오류가 난 것을 보면 min/max 매크로 함수와 꼬인 것이 틀림없어 보입니다. 실제로 Windows 쪽 헤더 파일을 포함하면 "minwindef.h" 파일이 자동적으로 추가되는데 다음과 같은 매크로가 포함되어 있어 문제가 발생한 것입니다.
// ========== minwindef.h
#ifndef NOMINMAX
#ifndef max
#define max(a,b) (((a) > (b)) ? (a) : (b))
#endif
#ifndef min
#define min(a,b) (((a) < (b)) ? (a) : (b))
#endif
#endif /* NOMINMAX */
헤더 파일에 나오듯이 해결 방법도 나오는데요. 즉, stdafx.h 헤더 파일 등에 "#define NOMINMAX"를 정의해 주면 끝!
[이 글에 대해서 여러분들과 의견을 공유하고 싶습니다. 틀리거나 미흡한 부분 또는 의문 사항이 있으시면 언제든 댓글 남겨주십시오.]