Home > Backend Development > C++ > body text

Why Do I Get Errors When Using std::min/max with #define NOMINMAX?

Patricia Arquette
Release: 2024-11-14 17:18:02
Original
257 people have browsed it

Why Do I Get Errors When Using std::min/max with #define NOMINMAX?

Using std::min/max with #define NOMINMAX

In a recent update to your main.cpp file, you introduced the following preprocessor directive:

#define NOMINMAX
#include <Windows.h>
#include <algorithm>
Copy after login

This action allows you to utilize the std::max and std::min functions within your code. However, subsequent attempts to employ these functions within other files yield errors such as:

error C2589: '(' : illegal token on right side of '::'
error C2059: syntax error : '::'
Copy after login

Despite attempts to define NOMINMAX in these additional files, the issue persists.

The issue lies in the fact that NOMINMAX defines aliases for the Windows min and max macros, overwriting the standard C versions provided by . To resolve this, use parentheses around the std::min and std::max calls:

(std::min)(x, y);
Copy after login

This approach avoids invoking the function-like macros, allowing the standard C versions to be applied.

The above is the detailed content of Why Do I Get Errors When Using std::min/max with #define NOMINMAX?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template