#include <algorithm>
#include <Windows.h>
int main()
{
int k = std::min(3, 4);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
什么是Windows正在做,如果我包括Windows.h我不能在Visual Studio 2005中使用std :: min.错误消息是:
error C2589: '(' : illegal token on right side of '::'
error C2059: syntax error : '::'
Run Code Online (Sandbox Code Playgroud) 所以我试图从cin获得有效的整数输入,并使用了这个问题的答案.
它建议:
#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);
}
Run Code Online (Sandbox Code Playgroud)
这给了我一个关于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 …Run Code Online (Sandbox Code Playgroud) 在我们的遗留代码中,以及我们的现代代码中,我们使用宏来执行代码生成等的漂亮解决方案.我们同时使用#和##运算符.
我很好奇其他开发人员如何使用宏来做很酷的事情,如果他们根本使用它们的话.
我见过的一类,其中一个叫成员变量min和max
class A
{
public:
A();
~A();
bool min;
bool max;
...
};
Run Code Online (Sandbox Code Playgroud)
用构造函数
A::A()
{
min=false;
max=true;
...
}
Run Code Online (Sandbox Code Playgroud)
我试图用初始化列表重写它:
A::A():min(false), max(true){}
Run Code Online (Sandbox Code Playgroud)
但是我收到了警告+错误
warning C4003: not enough actual parameters for macro 'min'
error C2059: syntax error : ')'
Run Code Online (Sandbox Code Playgroud)
因为min宏是在中定义的WinDef.h
在没有重命名成员变量的情况下,是否可以在这种情况下使用初始化列表?