CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Dec 2010
    Posts
    907

    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

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,395

    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

  3. #3
    Join Date
    Dec 2010
    Posts
    907

    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

  4. #4
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Cannot use global scope "max" function even ::max is used.

    Quote Originally Posted by lucky6969b View Post
    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

  5. #5
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Cannot use global scope "max" function even ::max is used.

    Quote Originally Posted by lucky6969b View Post
    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
  •  





Click Here to Expand Forum to Full Width

Featured