gpt4 book ai didi

c++ - 如何处理 windows.h 中的 max 宏与 std 中的 max 冲突?

转载 作者:太空狗 更新时间:2023-10-29 23:13:38 34 4
gpt4 key购买 nike

所以我试图从 cin 获取有效的整数输入,并使用了这个 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 宏没有那么多参数。这意味着我必须这样做

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 的定义并在之后重新定义它?

最佳答案

定义宏NOMINMAX :

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

关于c++ - 如何处理 windows.h 中的 max 宏与 std 中的 max 冲突?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37937870/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com