Search code examples
visual-studiovisual-c++

C Error - macro "max" requires 2 arguments, but only 1 given


While trying to compile the code below:

template <class T>
struct scalar_log_minimum {
    public:
        typedef T value_type;
        typedef T result_type;
        static
            result_type initial_value(){
                return std::log(std::numeric_limits<result_type>::max());
        }
        static
            void update(result_type& t, const value_type& x){
                if ( (x>0) && (std::log(x)<t) ) t = std::log(x);
        }
    };

I got the following error :

functional_ext.hpp:55:59: macro "max" requires 2 arguments, but only 1 given

Here "max" is not a macro, right? Then what is this error? BTW, I am using Visual Studio 2005.

Also what does 55:59 mean - 55 is the line number 59?


Solution

  • I find the many #defines that you encounter once you included windows.h very disturbing (not only max and min, but I also had problems with other generic words like Rectangle if I'm not mistaken). Therefore, I have developed the habit to include windows.h only when absolutely necessary, and never in header files. This reduces the pain to a small number of C++ files that are platform-specific.

    Unfortunately some boost libraries (I believe thread and asio) do include windows.h in their headers, and I still run into this kind of silly problems from time to time.

    My solution for the remainder of the situations where this causes problems is to #undef the problematic symbols after the inclusion of the header files.