Archived

This topic is now archived and is closed to further replies.

Trouble with max()

This topic is 5310 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Does anyone else have any idea why this C++ code snippet doesn''t seem to want to compile? I keep getting this compiler error. C:\Programming\Learn\Learn1\Learn1.cpp(12) : error C2065: ''max'' : undeclared identifier
#include <algorithm>
#include <iostream>

using namespace std;


int main()
{
	double x = 5;
	double y = 10;

	double z = max(x, y);
	cout << z;

	return 0;
}

Thanks for your help.

Share this post


Link to post
Share on other sites
i dont think the macro max is defined, its what it says. Include windows.h or make a max macro.

#define max(a,b) (((a) > (b)) ? (a) : (b))

Share this post


Link to post
Share on other sites
quote:
Original post by Daivam
Isn''t max() defined in the algorithm header file though?

Yes, it is defined in algorithm.

Are you sure that in the code that you are compiling you actually put:

using namespace std;

as the error may come as a result of max() not being qualified.

[ Google || Start Here || ACCU || MSDN || STL || GameCoding || BarrysWorld || E-Mail Me ]

Share this post


Link to post
Share on other sites
quote:
Original post by phosphorous
Maybe I''ve never tried it before without the .h at the end, but doesnt it need to be:

#include <algorithm.h>
#include <iostream.h>

?

Phosphorous

No! It should definitely be algorithm and iostream, i.e. without the .h extension.

[ Google || Start Here || ACCU || MSDN || STL || GameCoding || BarrysWorld || E-Mail Me ]

Share this post


Link to post
Share on other sites
quote:
Original post by Daivam
Isn''t max() defined in the algorithm header file though?

MS define min and max macros, so they leave them out of algorithm. Consult the MSDN documentation for further details.

Share this post


Link to post
Share on other sites
quote:
Original post by Lektrix
quote:
Original post by phosphorous
Maybe I''ve never tried it before without the .h at the end, but doesnt it need to be:

#include <algorithm.h>
#include <iostream.h>

?

Phosphorous

No! It should definitely be algorithm and iostream, i.e. without the .h extension.



Oops! You are absolutely right. I didn''t look close enough to realize that he needed to use STL headers. Guess I shouldn''t post before my morning Mt.Dew

Share this post


Link to post
Share on other sites