MAX/MIN function macro definition problem in C + + __jquery

Source: Internet
Author: User

Today encountered a modest problem, but it has been a day, but also accumulated experience and lessons. Usually when the code is always around the problem, as long as the final function even if completed, in fact, every bug is a good opportunity to further understand C + +, now the problem is relatively simple, it should be better to ponder, so as not to have a big problem in the future when do not know where to begin. OK, no more nonsense, go straight to the point.

It was just recently that most of my own work was solved, so I was able to take the time to look at the code sent to me by a classmate from the project team. My habit is to take the code to compile first, run to see what the current phenomenon, so as to debug the time to find out where the problem lies. However, this time even the compilation did not pass, I think it is very strange, she could not compile the code to me. By locating the error, I found that there was a phrase in "modulation.cpp"

beta = max (betamin, Min (isr* (y0-y1) + y0, 1));

Shows that the Max and Min functions are undefined.

I sent her QQ, she said in her platform can be compiled through. Her platform is VS2012, and my platform is VS2010. Is it really a platform problem? I googled it and found that the common solution was to add the following header file

#include <algorithm>
using namespace std;

I tried and found no way, or there is red line below. Looking for a long time, finally found the max and Min macro definition of the header file "Stdlib.h." I opened this file and the relevant paragraphs are as follows

/* Minimum and Maximum macros/*

__max (a,b)  (((a) > (b))? (a): (b)
#define __MIN (a,b) ((  a) < (b))? (a): (b))
/* Non-ansi names for compatibility */

#ifndef __cplusplus
#define MAX ((    a) > (b))? (a): (b)
#define MIN (a,b) ((    a) < (b))? (a): (b))
#endif

There's a macro definition of Max and Min. But why not use it. I'm trying to fix it.

/* Non-ansi names for compatibility *

//#ifndef __cplusplus
#define MAX ((    a) > (b))? (a): (b)
#define MIN (a,b) ((    a) < (b))? (a): (b))
//#endif

The red line disappears. Then I asked her for her "stdlib.h". Two file size is not the same, I am 46kb, her 53kb, proved that indeed from VS2010 evolution to VS2012 is added to the definition or function, there is no time to scrutinize, I directly to the location of the relevant functions. The first place code is the same, the second place is changed

/* Non-ansi names for compatibility *

//#ifndef __cplusplus
#define MAX ((    a) > (b))? (a): (b)
#define MIN (a,b) ((    a) < (b))? (a): (b))
//#endif/  * __cplusplus * *

even commented on it. In order to confirm that the source file itself is commented out, I continue to search for this file on Google, but the internet seems to have no relevant instructions, and did not see which download VS2012 under the "stdlib.h" file. Fortunately our class has classmate's computer uses is VS2012, therefore asked him to have a copy, discovers the source file does not annotate these two words, explained this annotation is her own addition. Here I am a little unhappy, change the head of the file is not in advance, feel not sincere let me help tune code. But this is a small problem, and I'm glad she didn't tell me, I will have this blog.

So I found the key to the problem and changed the code to

Beta = __max (betamin, __min (isr* (y0-y1) + y0, 1));

Compilation succeeded.

The 17 floor of the connection below gives me a more satisfying explanation.
http://bbs.csdn.net/topics/30080771
The copy is as follows: First, Max is a function rather than a macro in standard C + +, and its actual definition in different compilation systems is as follows:
Linux g++, header file

Template <class _tp>
inline const _tp& MAX (const _tp& __a, const _tp& __b) {
  return  __a < __b? __b: __a;
}

Windows VC6, header files (latest vs.net haven't tried, dare not to be raved)

Template<class _ty> inline
const _ty& _cpp_max (const _ty& _x, const _ty& _y)
{return (_x < _y ? _y: _x); }

Did you see it? g++ defined it as Max () according to standard C + +, and VC6 made it into _cpp_max ().
In fact, the source code attached to my question was in line with the standard C + + rules, and was compiled smoothly under g++, and because VC6 turned Max () into _cpp_max (), it failed to compile.
To use Max () in VC6, you can only retire with _cpp_max (). or _max ().
Because in the header file algorithm contains another header file xutility There is a sentence #define _max _cpp_max
There is also a related macro __max, defined in Stdlib.h: #define __MAX ((a) > (b))? (a): (b)), but __max is not standard C + +, which was left by the former C.

By the way, why use C + + example of the algorithm library is not, carefully looked at the small red line of the explanation, did not find the corresponding template, indicating that there is this function, but the data type does not match, changed to the following form on it.

beta = max (betamin, Min (isr* (y0-y1) + y0, 1.0));

To sum up, this time the problem is not big, but I harvested a lot. The biggest feeling is not easy to change from the library function, if the change must be with the cooperation of the people said, otherwise really harm others also harm themselves, in addition, write code must be standardized. Otherwise there will be a lot of strange problems, such as the last 1 to 1.0, as a qualified farmers, the most basic data type conversion must be noted.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.