help me with this error error C2668

What is the error in my code ?
IntelliSense: more than one instance of overloaded function "max" matches the argument list:

error C2668: 'max' : ambiguous call to overloaded function


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include<iostream>
using namespace std ;

template <class T >
T max ( T a , T b )
{
	if ( a>b)
		return a ;
	return b;
}

void main ()
{
	cout << max(10 , 20) ;
}
@mutexe thank you ^^

Answer : " std has a max function so that is where conflict stems from. "

so I just renamed max function to max1 and the problem solved :)
You could of course, use the scope operator to explicitly state that it's your max you're calling and not the one in the std namespace.

1
2
3
4
int main ()
{
    std::cout << ::max(10,20);
}
You could of course, use the scope operator to explicitly state that it's your max you're calling and not the one in the std namespace.

Or, even more sensibly, put your version of max into a namespace defined by you, so that you can disambiguate it by using your namespace.

Which, after all, is the whole point of namespaces.
Topic archived. No new replies allowed.