Function Templates error in Visual Studio 2003 -- Help Please

S

SteveM

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 ------------------------
 
M

Martin Vorbrodt

SteveM said:
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 ------------------------

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

SteveM

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
 
D

deane_gavin

SteveM said:
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>

tchar.h is presumably a compiler specific header. Do you need it?
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 );
}

As has already been mentioned, you should prefer std::max in
int _tmain(int argc, _TCHAR* argv[])

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()
{

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

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top