"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > Why Do I Get Errors When Using std::min/max with #define NOMINMAX?

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

Published on 2024-12-22
Browse:267

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 
#include 

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 : '::'

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);

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

Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3