overloading error for C++ template function











up vote
0
down vote

favorite












I am trying to do some practice with function templates as in the following example:



#include <iostream>
using namespace std;

template <class T>
T max(T a, T b)
{
return a > b ? a : b;
}

int main()
{
cout << "max(10, 15) = " << max(10, 15) << endl;

retun 0;

}


But I got the following errors. Could anybody recognize where the problem is?



..srcmain.cpp:59:40: error: call of overloaded 'max(int, int)' is   
ambiguous
cout << "max(10, 15) = " << max(10, 15) << endl;
^
..srcmain.cpp:16:3: note: candidate: 'T max(T, T) [with T = int]'
T max(T a, T b)
^~~
In file included from c:mingwincludec++8.1.0bitschar_traits.h:39,
from c:mingwincludec++8.1.0ios:40,
from c:mingwincludec++8.1.0ostream:38,
from c:mingwincludec++8.1.0iostream:39,
from ..srcmain.cpp:9:
c:mingwincludec++8.1.0bitsstl_algobase.h:219:5: note:
candidate: 'constexpr const _Tp& std::max(const _Tp&, const _Tp&)
[with _Tp = int]'
max(const _Tp& __a, const _Tp& __b)


I am sorry I am new to templates. Thanks for your help.










share|improve this question






















  • max returns bool not T
    – michelson
    Nov 10 at 18:17






  • 1




    Get rid of using namespace std; and you won't clash with std::max.
    – Jesper Juhl
    Nov 10 at 18:24












  • @michelson It most certainly does not.
    – Jesper Juhl
    Nov 10 at 18:26






  • 1




    Or stop naming your functions max or min, or anything else that sounds generic. There is a good chance that using these often-used names as identifiers will lead to a name clash somewhere, whether it is in the standard headers, or some third-party code that isn't yours.
    – PaulMcKenzie
    Nov 10 at 18:28















up vote
0
down vote

favorite












I am trying to do some practice with function templates as in the following example:



#include <iostream>
using namespace std;

template <class T>
T max(T a, T b)
{
return a > b ? a : b;
}

int main()
{
cout << "max(10, 15) = " << max(10, 15) << endl;

retun 0;

}


But I got the following errors. Could anybody recognize where the problem is?



..srcmain.cpp:59:40: error: call of overloaded 'max(int, int)' is   
ambiguous
cout << "max(10, 15) = " << max(10, 15) << endl;
^
..srcmain.cpp:16:3: note: candidate: 'T max(T, T) [with T = int]'
T max(T a, T b)
^~~
In file included from c:mingwincludec++8.1.0bitschar_traits.h:39,
from c:mingwincludec++8.1.0ios:40,
from c:mingwincludec++8.1.0ostream:38,
from c:mingwincludec++8.1.0iostream:39,
from ..srcmain.cpp:9:
c:mingwincludec++8.1.0bitsstl_algobase.h:219:5: note:
candidate: 'constexpr const _Tp& std::max(const _Tp&, const _Tp&)
[with _Tp = int]'
max(const _Tp& __a, const _Tp& __b)


I am sorry I am new to templates. Thanks for your help.










share|improve this question






















  • max returns bool not T
    – michelson
    Nov 10 at 18:17






  • 1




    Get rid of using namespace std; and you won't clash with std::max.
    – Jesper Juhl
    Nov 10 at 18:24












  • @michelson It most certainly does not.
    – Jesper Juhl
    Nov 10 at 18:26






  • 1




    Or stop naming your functions max or min, or anything else that sounds generic. There is a good chance that using these often-used names as identifiers will lead to a name clash somewhere, whether it is in the standard headers, or some third-party code that isn't yours.
    – PaulMcKenzie
    Nov 10 at 18:28













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I am trying to do some practice with function templates as in the following example:



#include <iostream>
using namespace std;

template <class T>
T max(T a, T b)
{
return a > b ? a : b;
}

int main()
{
cout << "max(10, 15) = " << max(10, 15) << endl;

retun 0;

}


But I got the following errors. Could anybody recognize where the problem is?



..srcmain.cpp:59:40: error: call of overloaded 'max(int, int)' is   
ambiguous
cout << "max(10, 15) = " << max(10, 15) << endl;
^
..srcmain.cpp:16:3: note: candidate: 'T max(T, T) [with T = int]'
T max(T a, T b)
^~~
In file included from c:mingwincludec++8.1.0bitschar_traits.h:39,
from c:mingwincludec++8.1.0ios:40,
from c:mingwincludec++8.1.0ostream:38,
from c:mingwincludec++8.1.0iostream:39,
from ..srcmain.cpp:9:
c:mingwincludec++8.1.0bitsstl_algobase.h:219:5: note:
candidate: 'constexpr const _Tp& std::max(const _Tp&, const _Tp&)
[with _Tp = int]'
max(const _Tp& __a, const _Tp& __b)


I am sorry I am new to templates. Thanks for your help.










share|improve this question













I am trying to do some practice with function templates as in the following example:



#include <iostream>
using namespace std;

template <class T>
T max(T a, T b)
{
return a > b ? a : b;
}

int main()
{
cout << "max(10, 15) = " << max(10, 15) << endl;

retun 0;

}


But I got the following errors. Could anybody recognize where the problem is?



..srcmain.cpp:59:40: error: call of overloaded 'max(int, int)' is   
ambiguous
cout << "max(10, 15) = " << max(10, 15) << endl;
^
..srcmain.cpp:16:3: note: candidate: 'T max(T, T) [with T = int]'
T max(T a, T b)
^~~
In file included from c:mingwincludec++8.1.0bitschar_traits.h:39,
from c:mingwincludec++8.1.0ios:40,
from c:mingwincludec++8.1.0ostream:38,
from c:mingwincludec++8.1.0iostream:39,
from ..srcmain.cpp:9:
c:mingwincludec++8.1.0bitsstl_algobase.h:219:5: note:
candidate: 'constexpr const _Tp& std::max(const _Tp&, const _Tp&)
[with _Tp = int]'
max(const _Tp& __a, const _Tp& __b)


I am sorry I am new to templates. Thanks for your help.







c++ eclipse templates






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 10 at 18:14









Spring19981

73




73












  • max returns bool not T
    – michelson
    Nov 10 at 18:17






  • 1




    Get rid of using namespace std; and you won't clash with std::max.
    – Jesper Juhl
    Nov 10 at 18:24












  • @michelson It most certainly does not.
    – Jesper Juhl
    Nov 10 at 18:26






  • 1




    Or stop naming your functions max or min, or anything else that sounds generic. There is a good chance that using these often-used names as identifiers will lead to a name clash somewhere, whether it is in the standard headers, or some third-party code that isn't yours.
    – PaulMcKenzie
    Nov 10 at 18:28


















  • max returns bool not T
    – michelson
    Nov 10 at 18:17






  • 1




    Get rid of using namespace std; and you won't clash with std::max.
    – Jesper Juhl
    Nov 10 at 18:24












  • @michelson It most certainly does not.
    – Jesper Juhl
    Nov 10 at 18:26






  • 1




    Or stop naming your functions max or min, or anything else that sounds generic. There is a good chance that using these often-used names as identifiers will lead to a name clash somewhere, whether it is in the standard headers, or some third-party code that isn't yours.
    – PaulMcKenzie
    Nov 10 at 18:28
















max returns bool not T
– michelson
Nov 10 at 18:17




max returns bool not T
– michelson
Nov 10 at 18:17




1




1




Get rid of using namespace std; and you won't clash with std::max.
– Jesper Juhl
Nov 10 at 18:24






Get rid of using namespace std; and you won't clash with std::max.
– Jesper Juhl
Nov 10 at 18:24














@michelson It most certainly does not.
– Jesper Juhl
Nov 10 at 18:26




@michelson It most certainly does not.
– Jesper Juhl
Nov 10 at 18:26




1




1




Or stop naming your functions max or min, or anything else that sounds generic. There is a good chance that using these often-used names as identifiers will lead to a name clash somewhere, whether it is in the standard headers, or some third-party code that isn't yours.
– PaulMcKenzie
Nov 10 at 18:28




Or stop naming your functions max or min, or anything else that sounds generic. There is a good chance that using these often-used names as identifiers will lead to a name clash somewhere, whether it is in the standard headers, or some third-party code that isn't yours.
– PaulMcKenzie
Nov 10 at 18:28












2 Answers
2






active

oldest

votes

















up vote
4
down vote













Your usage of templates is correct, but the compiler complains that there already is a function called max with same arguments.



It's full name would be std::max, but because you wrote using namespace std its just max and compiler cannot know which function to call.



Solution is not to use using, see Why is "using namespace std" considered bad practice? .






share|improve this answer




























    up vote
    1
    down vote













    using namespace std; is the issue



    Please stop using that, see why



    The iostream header includes another header file that pulls std::max, giving a compiler error.






    share|improve this answer





















    • Thanks for everybody's help. My problem has been solved.
      – Spring19981
      Nov 10 at 18:34











    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53241988%2foverloading-error-for-c-template-function%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    4
    down vote













    Your usage of templates is correct, but the compiler complains that there already is a function called max with same arguments.



    It's full name would be std::max, but because you wrote using namespace std its just max and compiler cannot know which function to call.



    Solution is not to use using, see Why is "using namespace std" considered bad practice? .






    share|improve this answer

























      up vote
      4
      down vote













      Your usage of templates is correct, but the compiler complains that there already is a function called max with same arguments.



      It's full name would be std::max, but because you wrote using namespace std its just max and compiler cannot know which function to call.



      Solution is not to use using, see Why is "using namespace std" considered bad practice? .






      share|improve this answer























        up vote
        4
        down vote










        up vote
        4
        down vote









        Your usage of templates is correct, but the compiler complains that there already is a function called max with same arguments.



        It's full name would be std::max, but because you wrote using namespace std its just max and compiler cannot know which function to call.



        Solution is not to use using, see Why is "using namespace std" considered bad practice? .






        share|improve this answer












        Your usage of templates is correct, but the compiler complains that there already is a function called max with same arguments.



        It's full name would be std::max, but because you wrote using namespace std its just max and compiler cannot know which function to call.



        Solution is not to use using, see Why is "using namespace std" considered bad practice? .







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 10 at 18:19









        Quimby

        52719




        52719
























            up vote
            1
            down vote













            using namespace std; is the issue



            Please stop using that, see why



            The iostream header includes another header file that pulls std::max, giving a compiler error.






            share|improve this answer





















            • Thanks for everybody's help. My problem has been solved.
              – Spring19981
              Nov 10 at 18:34















            up vote
            1
            down vote













            using namespace std; is the issue



            Please stop using that, see why



            The iostream header includes another header file that pulls std::max, giving a compiler error.






            share|improve this answer





















            • Thanks for everybody's help. My problem has been solved.
              – Spring19981
              Nov 10 at 18:34













            up vote
            1
            down vote










            up vote
            1
            down vote









            using namespace std; is the issue



            Please stop using that, see why



            The iostream header includes another header file that pulls std::max, giving a compiler error.






            share|improve this answer












            using namespace std; is the issue



            Please stop using that, see why



            The iostream header includes another header file that pulls std::max, giving a compiler error.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 10 at 18:19









            P0W

            32.6k74692




            32.6k74692












            • Thanks for everybody's help. My problem has been solved.
              – Spring19981
              Nov 10 at 18:34


















            • Thanks for everybody's help. My problem has been solved.
              – Spring19981
              Nov 10 at 18:34
















            Thanks for everybody's help. My problem has been solved.
            – Spring19981
            Nov 10 at 18:34




            Thanks for everybody's help. My problem has been solved.
            – Spring19981
            Nov 10 at 18:34


















             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53241988%2foverloading-error-for-c-template-function%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Full-time equivalent

            Bicuculline

            さくらももこ