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?
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
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.
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.