Function Templates error in Visual Studio 2003 -- Help Please

Discussion in 'C++' started by SteveM, Oct 21, 2005.

  1. SteveM

    SteveM Guest

    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 ------------------------
    SteveM, Oct 21, 2005
    #1
    1. Advertising

  2. "SteveM" <> wrote in message
    news:...
    > 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.
    Martin Vorbrodt, Oct 21, 2005
    #2
    1. Advertising

  3. SteveM

    SteveM Guest

    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
    SteveM, Oct 21, 2005
    #3
  4. SteveM

    Guest

    SteveM wrote:
    > 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
    <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.

    > 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
    , Oct 21, 2005
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. wh
    Replies:
    2
    Views:
    495
    Cowboy \(Gregory A. Beamer\)
    Jan 16, 2004
  2. rockdale
    Replies:
    1
    Views:
    589
    Juan T. Llibre
    Aug 23, 2006
  3. xman
    Replies:
    0
    Views:
    468
  4. xman
    Replies:
    1
    Views:
    567
    Victor Bazarov
    Aug 17, 2005
  5. recover
    Replies:
    2
    Views:
    789
    recover
    Jul 25, 2006
Loading...

Share This Page