conectionist
conectionist

Reputation: 2924

Function returning reference not working with window.h

1: #include <windows.h>

2: int& max(int& a, int& b)
3: {
4:   return a > b ? a : b;
5: }

6: int main()
7: {
8:   return 0;
9: }

Visual Studio 2008 Express Edition shouts:

1>e:...\main.cpp(2) : error C2062: type 'int' unexpected

1>e:...\main.cpp(2) : error C2062: type 'int' unexpected

1>e:...\main.cpp(2) : error C2059: syntax error : ')'

1>e:...\main.cpp(3) : error C2143: syntax error : missing ';' before '{'

1>e:...\main.cpp(3) : error C2447: '{' : missing function header (old-style formal list?)

It seems to work if I replace windows.h with stdio.h or iostream (or if I remove it)

Why is this?

Upvotes: 2

Views: 228

Answers (1)

user93353
user93353

Reputation: 14039

#include <windows.h>

#undef min
#undef max

int & max(int& a, int& b)
{
    return a > b ? a : b;
}
int main()
{
    return 0;
}

<windows.h> defines macros for max and min which interfere with yours.

Other ways

  • Rename your functions.

  • use NOMINMAX. This is the common solution recommended for using some STL headers which define min and max themselves.

    #define NOMINMAX
    #include <windows.h>
    

Upvotes: 5

Related Questions