std::max - expected an identifier
Hazarding a guess, since you're using VC++ – put this before any #include
s:
#define NOMINMAX
windows.h
defines macros named min
and max
like so:
#define min(a,b) (((a) < (b)) ? (a) : (b))
#define max(a,b) (((a) > (b)) ? (a) : (b))
The Windows SDK has contained these macros since before C++ was standardized, but because they obviously play havoc with the C++ standard library, one can define the NOMINMAX
macro to prevent them from being defined.
As a rule, if you're using C++ (as opposed to C) and including windows.h
, always define NOMINMAX
first.
std::cin.ignore(std::numeric_limitsstd::streamsize::max(), '\n') error when using #include Windows.h
The <windows.h>
header has had the min()
and max()
macros since time immemorial, and they frequently cause problems with C++. Fortunately, you can disable them by adding #define NOMINMAX
before including <windows.h>
.
Problem calling std::max
You are probably including windows.h
somewhere, which defines macros named max
and min
.
You can #define NOMINMAX
before including windows.h
to prevent it from defining those macros, or you can prevent macro invocation by using an extra set of parentheses:
column = (std::max)(1u, column + count);
Why is std::min failing when windows.h is included?
The windows.h
header file (or more correctly, windef.h
that it includes in turn) has macros for min
and max
which are interfering.
You should #define NOMINMAX
before including it.
In fact, you should probably do that even if there were no conflict, since the naive definition of the macro shows why function-like macros are a bad idea:
#define max(a,b) ((a)>(b)?(a):(b))
If you invoke that macro with, for example:
int x = 5, y = 10;
int c = max(x++, y--);
then y
will not end up with what you expect. For example, it will expand to:
int c = ((x++)>(y--)?(x++):(y--));
That expression (unless undefined behaviour kicks in which would be even worse) will decrement y
twice, not something you're likely to expect.
I basically use macros only for conditional compilation nowadays, the other two major use cases of old (symbolic constants and function-like macros) are better handled with more modern language features (real enumerated types and inline function suggestion).
Syntax error with std::numeric_limits::max
Your problem is caused by the <Windows.h>
header file that includes macro definitions named max
and min
:
#define max(a,b) (((a) > (b)) ? (a) : (b))
Seeing this definition, the preprocessor replaces the max
identifier in the expression:
std::numeric_limits<size_t>::max()
by the macro definition, eventually leading to invalid syntax:
std::numeric_limits<size_t>::(((a) > (b)) ? (a) : (b))
reported in the compiler error: '(' : illegal token on right side of '::'
.
As a workaround, you can add the NOMINMAX
define to compiler flags (or to the translation unit, before including the header):
#define NOMINMAX
or wrap the call to max
with parenthesis, which prevents the macro expansion:
size_t maxValue_ = (std::numeric_limits<size_t>::max)()
// ^ ^
or #undef max
before calling numeric_limits<size_t>::max()
:
#undef max
...
size_t maxValue_ = std::numeric_limits<size_t>::max()
Error: Expected Identifier
You defined Mob
to... nothing. That makes your code equivalent to:
class {
private:
int lvl;
float hp;
public:
(int, float); // Expecting an identifier indeed
};
and that holds for the rest of the code where #define Mob
is included.
If you're trying to make include guards, you need a unique name and define it conditionaly:
#ifndef UNIQUE_MOB
#define UNIQUE_MOB
// code
#endif
#define NOMINMAX using std::min/max
If you're really desperate, put parentheses around the function names:
(std::min)(x, y);
This syntax won't apply a function-like macro. (Formally, to apply a function-like macro the name of the macro must be followed by optional white space then a '('.)
Related Topics
C++ Templates Declare in .H, Define in .Hpp
C++ Implicit Conversion (Signed + Unsigned)
What Is Activation Record in the Context of C and C++
How to Write a Stateful Allocator in C++11, Given Requirements on Copy Construction
How to Create/Read/Write JSON Files in Qt5
Redirecting/Redefining Print() for Embedded Lua
Does Auto Deduce the Type at Compile Time or Runtime in C++ 11
Why Do We Use Std::Function in C++ Rather Than the Original C Function Pointer
Where in Memory Is Vtable Stored
Getline Keeps on Getting Newline Character. How to Avoid This
How to Pass a C++ Lambda to a C-Callback That Expects a Function Pointer and a Context
Prolonging the Lifetime of Temporaries
C/C++ How to Copy a Multidimensional Char Array Without Nested Loops
Should 'Const' and 'Constexpr' Variables in Headers Be 'Inline' to Prevent Odr Violations
Why Do Functions/Objects Inside Anonymous Namespace Have External Linkage