| re: Function Templates error in Visual Studio 2003 -- Help Please
SteveM wrote:[color=blue]
> I am trying to learn C++ (taking classes at the University of
> Washington) but have run across a bit of confusion with respect to
> function templates. It appears that VS 2003 has a different way of
> looking at things and is causing the folliwing errors when I try to
> build the code excerpt below:
>
> d:\C++ Projects\TemplatesTest\TemplatesTest\TemplatesTest .cpp(38):
> error C2668: 'max' : ambiguous call to overloaded function
> d:\C++ Projects\TemplatesTest\TemplatesTest\TemplatesTest .cpp(39):
> error C2668: 'max' : ambiguous call to overloaded function
> d:\C++ Projects\TemplatesTest\TemplatesTest\TemplatesTest .cpp(41):
> error C2668: 'max' : ambiguous call to overloaded function
>
> Now I understand from researching this that this is a known bug with
> the way that Microsoft VS 2003 compilers work... my question here is
> what can I do to get around this. How do I need to change the code in
> order to get the results I want (according to the example) and satisfy
> the restrictions being placed on the code by the compiler.
>
>
> HELP!!!!!! Please :-)
>
>
> Thanks, Steve
>
>
> ------ Code excerpt --------------------
>
> #include <iostream>
> #include <tchar.h>[/color]
tchar.h is presumably a compiler specific header. Do you need it?
[color=blue]
>
> using namespace std;
>
> //max returns the maximum of the two elements
> template <class T>
> T max(T a, T b)
> {
> return ((a > b )? a : b );
> }[/color]
As has already been mentioned, you should prefer std::max in
<algorithm>. Unless, of course, you've been set an exercise to write
your own - in which case just be aware that the standard one exists.
[color=blue]
> int _tmain(int argc, _TCHAR* argv[])[/color]
I am guessing tchar.h allows you to use non-standard _tmain and _TCHAR,
but is there any reason not to use one of these?
int main(int argc, char* argv[])
or even just
int main()
[color=blue]
> {
>
> cout << "max((10, 15) = " << max(10, 15) << endl ;
> cout << "max('k', 's') = " << max('k', 's') << endl ;
> cout << "max(10.1, 15.2) = "
> << max(10.1, 15.2) << endl ;
>
>
> return 0;
> }[/color]
Remove the compiler/windows specific stuff and see if it makes the
problem go away. With the changes I've suggested, the code compiles
fine on Comeau online.
If, for some reason you've not given yet, you need the windows header
and code, you'll need to ask a newsgroup relevant to your compiler
whether there is a workaround for your problem.
Gavin Deane |