ambiguous call to overloaded function

Hi Folks

The following call to max(.) creates the error mentioned in the topic

1
2
3
4
5
6
template <class T> void Vec<T>::grow()
{
	// when growing, allocate twice as much space as currently in use
	size_type new_size = max( 2 * (limit - data) , ptrdiff_t(1) );
...
}


The function max(.) is defined as

1
2
3
4
template <class T> T max(const T& a, const T& b)
{
	return (a > b) ? a : b; 
}


and here is the full error message

c:\dev\acccpp_14_4\acccpp_14_4\vec.h(128): error C2668: 'max' : ambiguous call to overloaded function
1>          c:\dev\acccpp_14_4\acccpp_14_4\vec.h(9): could be 'T max<int>(const T &,const T &)'
1>          with
1>          [
1>              T=int
1>          ]
1>          c:\program files\microsoft visual studio 10.0\vc\include\xutility(2078): or       'const _Ty &std::max<int>(const _Ty &,const _Ty &)'
1>          with
1>          [
1>              _Ty=int
1>          ]
1>          while trying to match the argument list '(int, ptrdiff_t)'
1>          c:\dev\acccpp_14_4\acccpp_14_4\vec.h(126) : while compiling class template member function 'void Vec<T>::grow(void)'
1>          with
1>          [
1>              T=char
1>          ]
1>          c:\program files\microsoft visual studio 10.0\vc\include\iterator(30) : while compiling class template member function 'std::back_insert_iterator<_Container> &std::back_insert_iterator<_Container>::operator =(const char &)'
1>          with
1>          [
1>              _Container=Vec<char>
1>          ]
1>          c:\dev\acccpp_14_4\acccpp_14_4\str.h(23) : see reference to class template instantiation 'std::back_insert_iterator<_Container>' being compiled
1>          with
1>          [
1>              _Container=Vec<char>
1>          ]


Could somebody please help and translate or explain or show a solution to the problem here?
Last edited on
There is std::max in algorithm
Somehow you are including it (and you put a using namespace std; so you are making it global).
Make sure both arguments are of the same type, or specify the template type when you call the function max<int>(...
The problem is that header <xutility> that is explicitly or implicitly included in your program also declares a template function with name max.
As it is clear that you are using directive using namespace std; and are calling unqualified name max the compiler does not know which function to select.

To avoid the error you should use qualified name of max, that is if you want that your function were called then write

size_type new_size = ::max( 2 * (limit - data) , ptrdiff_t(1) );

that is use qualified name ::max

If you want that the standard function were called then write

size_type new_size = std::max( 2 * (limit - data) , ptrdiff_t(1) );


that is use qualified name std::max
Last edited on
Changing the code from max to ::max solved the problem!

Thanks guys and special thanks to vlad from moscow!

My understanding is now that ::max restricts the search for a function max to the global namespace and that the compiler is not allowed to search in any other namespace e.g. namespace std anymore.

Is this correct?
My understanding is now that ::max restricts the search for a function max to the global namespace and that the compiler is not allowed to search in any other namespace e.g. namespace std anymore.

Is this correct?


It is valid provided that global namespace contains declaration of the searched function. Otherwise the compiler will look all namespaces that were included in the global namespace with directive using namespace.

Look an example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>

namespace One
{
void f() { std::cout << "One::f\n"; }
}

namespace Two
{
using namespace One;

void g() { Two::f(); }

void f() { std::cout << "Two::f\n"; }
}

int main()
{
   Two::g();
   Two f();
}
Last edited on
Thanks again to vlad from mosow!
Topic archived. No new replies allowed.