Solved

std::min and std::max definitions

Posted on 2007-03-26
5
5,295 Views
Last Modified: 2012-06-21
I have the following statements in my stdafx.h file:

using std::min;
using std::max;

My compiler is telling me that min and max are not members of std.

What am I missing?

I am using VS 6.0 and have included the following:

atlbase.h
sql.h
sqlext.h
vector
list
cmath
time.h
algorithm
process.h
0
Comment
Question by:StanChart
  • 2
  • 2
5 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 18793447
Try

#include <xutility>
using std::_cpp_min;
using std::_cpp_max;
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 18793641
MSDN that came with VC6 says:

------------------------------------------------------------
min
template<class T>
    const T& min(const T& x, const T& y);
template<class T, class Pred>
    const T& min(const T& x, const T& y, Pred pr);
The first template function returns y if y < x. Otherwise it returns x. T need supply only a single-argument constructor and a destructor.

The second template function behaves the same, except that it replaces operator<(X, Y) with pr(X, Y).

Microsoft-Specific:
To avoid conflicts with min and max in WINDEF.H, use _MIN and _MAX instead. These macros evaluate to _cpp_min and _cpp_max, respectively.
END Microsoft-Specific
------------------------------------------------------------

So you either use jkr's solution or take _MIN and _MAX .

Regards, Alex
0
 

Author Comment

by:StanChart
ID: 18794209
I'm actually merging some code right now and what I previously had was:

using namespace std;

If I replace that with using std::_cpp_min/max, I get other errors like auto_ptr not defined, etc.
If I include both, I get "none of the 2 overloads have a best conversion".

The rest of the code base uses the std::min/max functions so I'd like to stick with those since it is not my code that i would be modifying.

Thanks
0
 
LVL 86

Expert Comment

by:jkr
ID: 18794274
And if you use

using namespace std;

it works?
0
 
LVL 39

Accepted Solution

by:
itsmeandnobodyelse earned 500 total points
ID: 18794513
>>>> The rest of the code base uses the std::min/max functions
Unfortunately, VC6 was released prior to C++ Standard, so its STL implementation is not compliant in many points.  

In the MSDN comment they told that there is a conflict with with (macro) definitions in WINDEF.H. You can get rid of that macro by adding NOMINMAX to the preprocessor macros of your project settings. Then in your cpp files you could add at the top (below inlcude of stdafx.h if using PCH).

#ifndef min
#define min_defined
#define min std::_cpp_min
#endif

#ifndef max
#define max_defined
#define max std::_cpp_max
#endif

and et the bottom of the cpp:

#ifdef min_defined
#undef min
#endif
#ifdef max_defined
#undef max
#endif

Of course you could put both sequences into a header file which can be included then.

Isn't it ugly? Maybe a better way is to copy the template definition of _ccp_min and _cpp_max, put them to a new header file and rename the function names to min and max accordingly. Put both function to the namespace std. If you uccessfully suppressed any other occurrence fo min and max you can include that header what should make valid the above usage clauses.

Regards, Alex




0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
simple way to manipulate chinese characters in non unicode mfc c++ application. 9 220
Why isn't object file created? 6 86
Need some help with mailto 16 31
boost::uuid crashes 17 35
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

713 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question