Connecting Tech Pros Worldwide Forums | Help | Site Map

Function Templates error in Visual Studio 2003 -- Help Please

SteveM
Guest
 
Posts: n/a
#1: Oct 21 '05
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>

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 );
}


int _tmain(int argc, _TCHAR* argv[])
{

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;
}

----------- End of Code excerpt ------------------------


Martin Vorbrodt
Guest
 
Posts: n/a
#2: Oct 21 '05

re: Function Templates error in Visual Studio 2003 -- Help Please


"SteveM" <steve.moody@philips.com> wrote in message
news:1129908968.594314.157770@g43g2000cwa.googlegr oups.com...[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>
>
> 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 );
> }
>
>
> int _tmain(int argc, _TCHAR* argv[])
> {
>
> 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;
> }
>
> ----------- End of Code excerpt ------------------------
>[/color]

i think the conflics exists because both stl and win32 define max. plus you
define one too. get rid of it! use stl!

#include <algorithm>
cout << std::max(10, 15) << endl;

also fully qualify the name with std:: to avoid name clashes with windows-MS
defined crap.

hope that helps.


SteveM
Guest
 
Posts: n/a
#3: Oct 21 '05

re: Function Templates error in Visual Studio 2003 -- Help Please


Yep that did the trick... duh! it never occured to me that this was a
name collision that I was dealing with. I was thinking that my lack of
understanding of templates was the culprit. I am back and working.
Thanks for setting me straight
I appreciate the help :-)
-Steve

deane_gavin@hotmail.com
Guest
 
Posts: n/a
#4: Oct 21 '05

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

Closed Thread