-
January 31st, 2013, 12:53 AM
#1
Cannot use global scope "max" function even ::max is used.
The compiler "forces" me to use the std::max method,
I cannot override it even when writing ::max in my code.
Any ideas would be nice.
Thanks
Jack
-
January 31st, 2013, 02:01 AM
#2
Re: Cannot use global scope "max" function even ::max is used.
The simplest way would to remove
Code:
using namespace std;
and add std:: prefix to all functions from std library.
See also http://stackoverflow.com/questions/1...-practice-in-c
Victor Nijegorodov
-
January 31st, 2013, 02:23 AM
#3
Re: Cannot use global scope "max" function even ::max is used.
Alrite Victor, changed it, but it didn't work, but I've worked around it by #defin'ing it in my header file.
Thanks
Jack
-
January 31st, 2013, 05:33 AM
#4
Re: Cannot use global scope "max" function even ::max is used.
Originally Posted by lucky6969b
Alrite Victor, changed it, but it didn't work, but I've worked around it by #defin'ing it in my header file.
Thanks
Jack
The problem is that the Windows header files defines min() and max() as macros. Attempting to mix them with the C++ standard std::min() and std::max() becomes problematic.
There are two solutions:
1) If your usage of min() and max() are in a CPP file (not a header file), and that source file extensively uses std::min/max, making changes risky, then you should do this:
Code:
#include (all of your Windows related headers)
#undef min
#undef max
#include <algorithm>
//...
// Your C++ source code
This undefined the macros, includes the C++ header that defines std::min/max. This effectively removes Visual C++'s version from the source module.
2) If you're using std::min/max in a header file, then you can't really use solution 1), since it may mess up other compilation units that may require the macro version of min() and max(). The workaround is to create your own macro/function called mymin()/mymax() (or similar named function), and use that in the header file.
Regards,
Paul McKenzie
-
January 31st, 2013, 05:38 AM
#5
Re: Cannot use global scope "max" function even ::max is used.
Originally Posted by lucky6969b
Alrite Victor, changed it, but it didn't work, but I've worked around it by #defin'ing it in my header file.
Thanks
Jack
Do not write any macro named min() and max() in a header file. Not only is there no need to, but it is very risky to do so. You're risking having modules not compile correctly.
Instead, work with what exists. Either you #undef the existing macros in a CPP source file and use std::min()/max(), or create brand new functions named differently than min() and max().
Regards,
Paul McKenzie
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|