我如何处理最大宏在Windows.h碰撞与max在std? [英] How do I deal with the max macro in windows.h colliding with max in std?

查看:130
本文介绍了我如何处理最大宏在Windows.h碰撞与max在std?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我试图从cin获取有效的整数输入,并使用此

So I was trying to get valid integer input from cin, and used an answer to this question.

建议:

#include <Windows.h> // includes WinDef.h which defines min() max()
#include <iostream>
using std::cin;
using std::cout;

void Foo()
{
    int delay = 0;
    do
    {
        if(cin.fail())
        {
            cin.clear();
            cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
        }
        cout << "Enter number of seconds between submissions: ";
    } while(!(cin >> delay) || delay == 0);
}

这在Windows上给我一个错误,说 max 宏不占用这么多参数。这意味着我必须这样做。

Which gives me an error on Windows, saying that the max macro doesn't take that many arguments. Which means I have to do this

do
{
    if(cin.fail())
    {
        cin.clear();
#undef max
        cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    }
    cout << "Enter number of seconds between submissions: ";
} while(!(cin >> delay) || delay == 0);

让它工作。这很丑陋;有没有更好的办法解决这个问题?也许我应该存储 max 的定义,然后重新定义?

To get it to work. That's pretty ugly; is there a better way to work around this issue? Maybe I should be storing the definition of max and redefining it afterward?

推荐答案

p>定义宏 NOMINMAX

Define the macro NOMINMAX:


这将禁止Windef.h中的min和max定义。

This will suppress the min and max definitions in Windef.h.

这篇关于我如何处理最大宏在Windows.h碰撞与max在std?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆