Template Method throws error for string

Hi,
Could anybody point me what is wrong wth this code. I get the following compiler error -
ambiguous call to overloaded function
1> c:\documents and settings\rsisondi\my documents\visual studio 2005\projects\templatefunction\templatefunction\templatefunction.cpp(14): could be 'const T &max<std::string>(const T &,const T &)'
1> with
1> [
1> T=std::string
1> ]

However Code works If I don't pass strings to max function.



It works if I comment
"std::string s3 = max(s1,s2);" line.


#include "stdafx.h"
#include<iostream>
#include<string>

template <typename T>
inline T const& max (T const& a, T const& b)
{
// if a < b then use b else use a
return a<b?b:a;
}

int main()
{
std::cout<< max(3,4)<<std::endl;
std::cout<<max(2.3,1.1)<<std::endl;
std::string s1= "Hello";
std::string s2= "World";
std::string s3 = max(s1,s2);
std::cout<<s3<<std::endl;
getchar();
return 0;
}
Interesting one. In short, I don't know.

The compiler can't decide whether to use yours or the standard one for std::string. You can force it to use yours with ::max or the standard one std::max.

I'll look into it further when I have a bit more time.
Last edited on
Topic archived. No new replies allowed.