Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Visual C++ not recognising min() and max() functions in the STL

Status
Not open for further replies.

Ipito

Programmer
Jun 16, 2005
4
GB
Hi,

I have a program which uses the max() and min() functions from the STL. The program compiles and runs under Unix using the g++ compiler; however, when I try to build it using Visual C++, I get a C2065 error message, which says " 'max' : undeclared identifier".

I have used the #include<algorithm> at the top, and am using namespace std, so I can't see what could be wrong -especially since it's standard C++, and the code will compile fine under a different compiler. I'd really appreciate any help.
 
Does a simple program work (this works for me on VC++ 6.0 with an updated STL implementation and on VC++ 7.1):
Code:
#include <iostream>
#include <algorithm>

using namespace std;

int main()
{
    cout << max(3, 5) << endl;
}
If that works, then you might want to show some pf your code. You might also consider specifying std::max since max is a commonly used name.
 
Thanks. I tried the code fragment you advised, and it still generates the same error message when I try to build the project. I then tried using the adding the scoping operator std::, and it gives the additional error message "error 2039 : 'max' is not a member of the std".

I'm also using VC++ 6.0, but (to my knowledge) I don't have the updated STL implementation. I've searched on the net, and there seems to be a number of updates available. Is there a particular update that you could recommend?

Cheers
 
Do you have the latest Visual C++ 6.0 service pack?
 
I never downloaded any service pack. I just have VC++ 6.0. I usually use g++, and I'm fairly new the VC++ compiler.

I tried the code fragment above witht the added define statement, but I still get the same error message (C2065).
 
I did some searching and this is a well-known problem (I'd seen it before but forgot about it). Microsoft had its own macros min and max before the standard was released, so when the standard included functions called min and max, they decided not to implement those. Since macros don't respect namespace restrictions they couldn't keep both.

Try one more time with _cpp_max() instead of std::max(). I was able to get this error on another machine with VC++ 6.0. When I switched to _cpp_max it worked.

There are several ways to upgrade your STL. Dinkumware has upgrades available on their site (Dinkumware made the default implementation). You can also use other STL implementations like STLPort. The easiest thing would be to use the latest Dinkumware one, though. If _cpp_max works for you then it might not be necessary. Also, installing the latest service pack might be all you need to do.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top