template program doesnot work

Discussion in 'C++' started by Sooraj S, Mar 26, 2013.

  1. Sooraj S

    Sooraj S Guest

    H, I am new to templates. Can anyone tell me why the program gives me
    the error?

    template <class T> T min(T a, T b)
    {
    if (a < b) { cout << a << "is minimum"; }
    else { cout << b << "is minimum"; }
    return (a<b) ? a:b;
    }
    int main()
    {
    min <int> (10,12);
    }

    OUTPUT:
    In function 'int main()':
    Line 9: error: call of overloaded 'min(int, int)' is ambiguous
    compilation terminated due to -Wfatal-errors.
     
    Sooraj S, Mar 26, 2013
    #1
    1. Advertising

  2. On Tuesday, March 26, 2013 9:59:29 PM UTC+1, Sooraj S wrote:
    > H, I am new to templates. Can anyone tell me why the program gives me
    > the error?
    >
    > template <class T> T min(T a, T b)
    > {
    > if (a < b) { cout << a << "is minimum"; }
    > else { cout << b << "is minimum"; }
    > return (a<b) ? a:b;
    > }
    >
    > int main()
    > {
    > min <int> (10,12);
    > }
    >
    > OUTPUT:
    > In function 'int main()':
    > Line 9: error: call of overloaded 'min(int, int)' is ambiguous
    > compilation terminated due to -Wfatal-errors.


    Your program is mostly ok, except for the missing definition of "cout".
    You should add:

    #include <iostream>
    using std::cout;

    Your program then works as you probably intended it to.

    I guess you did include several library headers and added
    using namespace std; This pulls cout into the global namespace
    but also all other standard library symbols. Among which is
    also std::min. This in turn led to the ambiguous overload error
    you got. Lesson learnt: never do using namespace std... Only use
    using for what you need or spell out std::cout explicitly.



    You probably
     
    Gert-Jan de Vos, Mar 26, 2013
    #2
    1. Advertising

  3. Sooraj S

    Stefan Ram Guest

    Sooraj S <> writes:
    >H, I am new to templates. Can anyone tell me why the program gives me
    >the error?


    »#include <iostream>« missing?
    »::std::« missing?
    Terminating the output line with a line terminator missing?

    >template <class T> T min(T a, T b)
    >{
    > if (a < b) { cout << a << "is minimum"; }
    > else { cout << b << "is minimum"; }
    > return (a<b) ? a:b;
    >}
    >int main()
    >{
    > min <int> (10,12);
    >}
     
    Stefan Ram, Mar 26, 2013
    #3
  4. Sooraj Sæ–¼ 2013å¹´3月27日星期三UTC+8上åˆ4時59分29秒寫é“:
    > H, I am new to templates. Can anyone tell me why the program gives me
    >
    > the error?
    >
    >
    >
    > template <class T> T min(T a, T b)
    >
    > {
    >
    > if (a < b) { cout << a << "is minimum"; }
    >
    > else { cout << b << "is minimum"; }
    >
    > return (a<b) ? a:b;
    >
    > }
    >
    > int main()
    >
    > {
    >
    > min <int> (10,12);
    >
    > }
    >
    >
    >
    > OUTPUT:
    >
    > In function 'int main()':
    >
    > Line 9: error: call of overloaded 'min(int, int)' is ambiguous
    >
    > compilation terminated due to -Wfatal-errors.


    Please try min <int> (a+b,c) for valid integers in your function.
     
    88888 Dihedral, Mar 26, 2013
    #4
  5. Sooraj S

    Ian Collins Guest

    88888 Dihedral wrote:
    >
    > Please try min <int> (a+b,c) for valid integers in your function.


    Why? The code was broken.

    Please learn to quote.

    --
    Ian Collins
     
    Ian Collins, Mar 26, 2013
    #5
  6. Sooraj S

    Ian Collins Guest

    Sooraj S wrote:
    > H, I am new to templates. Can anyone tell me why the program gives me
    > the error?
    >
    > template <class T> T min(T a, T b)
    > {
    > if (a < b) { cout << a << "is minimum"; }
    > else { cout << b << "is minimum"; }
    > return (a<b) ? a:b;
    > }
    > int main()
    > {
    > min <int> (10,12);


    In addition to the other corrections, you shouldn't need to specify the
    type, the compiler will be able to deduce it.

    min(10,12);

    will suffice.

    --
    Ian Collins
     
    Ian Collins, Mar 26, 2013
    #6
  7. Sooraj S

    Haochen Xie Guest

    Sooraj S wrote:
    > H, I am new to templates. Can anyone tell me why the program gives me
    > the error?
    >
    > template<class T> T min(T a, T b)
    > {
    > if (a< b) { cout<< a<< "is minimum"; }
    > else { cout<< b<< "is minimum"; }
    > return (a<b) ? a:b;
    > }
    > int main()
    > {
    > min<int> (10,12);
    > }
    >
    > OUTPUT:
    > In function 'int main()':
    > Line 9: error: call of overloaded 'min(int, int)' is ambiguous
    > compilation terminated due to -Wfatal-errors.
    >


    You might included <algorithm> (which provides std::min basically doing
    exactly what your template is doing) and used namespace std; so that the
    compiler doesn't know whether it should choose the min you wrote or the
    one in the STL. So to get around it, just add namespace specifier to the
    function call. Use "::min<int>(10,12);" to call your own version or
    "std::min<int>(10,12)" to use the STL version.

    If you post all your codes next time when asking a question, that would
    be great.

    --
    Posted by Haochen Xie
     
    Haochen Xie, Mar 27, 2013
    #7
  8. Sooraj S

    Haochen Xie Guest

    Ian Collins wrote:
    > 88888 Dihedral wrote:
    >>
    >> Please try min <int> (a+b,c) for valid integers in your function.

    >
    > Why? The code was broken.
    >
    > Please learn to quote.
    >


    Never mind what 88888 says. That's just some not necessarily true, and
    random stuff...

    --
    Posted by Haochen Xie
     
    Haochen Xie, Mar 27, 2013
    #8
  9. Haochen Xieæ–¼ 2013å¹´3月27日星期三UTC+8下åˆ12時42分39秒寫é“:
    > Ian Collins wrote:
    >
    > > 88888 Dihedral wrote:

    >
    > >>

    >
    > >> Please try min <int> (a+b,c) for valid integers in your function.

    >
    > >

    >
    > > Why? The code was broken.

    >
    > >

    >
    > > Please learn to quote.

    >
    > >

    >
    >
    >
    > Never mind what 88888 says. That's just some not necessarily true, and
    >
    > random stuff...
    >
    >
    >
    > --
    >
    > Posted by Haochen Xie


    Kind of boring to filter out those who failed to add
    in every generations of programming languages.
     
    88888 Dihedral, Mar 27, 2013
    #9
  10. Sooraj S

    Ian Collins Guest

    88888 Dihedral wrote:
    > Haochen Xieæ–¼ 2013å¹´3月27日星期三UTC+8下åˆ12時42分39秒寫é“:
    >>
    >>
    >> Never mind what 88888 says. That's just some not necessarily true, and
    >>
    >> random stuff...
    >>
    >>
    >>
    >> --
    >>
    >> Posted by Haochen Xie

    >
    > Kind of boring to filter out those who failed to add
    > in every generations of programming languages.


    That proves the point and it still can't learn to quote.

    --
    Ian Collins
     
    Ian Collins, Mar 27, 2013
    #10
  11. Sooraj S

    SG Guest

    Am 26.03.2013 21:59, schrieb Sooraj S:
    > H, I am new to templates. Can anyone tell me why the program gives me
    > the error?
    >
    > template <class T> T min(T a, T b)
    > {
    > if (a < b) { cout << a << "is minimum"; }
    > else { cout << b << "is minimum"; }
    > return (a<b) ? a:b;
    > }
    > int main()
    > {
    > min <int> (10,12);
    > }
    >
    > OUTPUT:
    > In function 'int main()':
    > Line 9: error: call of overloaded 'min(int, int)' is ambiguous
    > compilation terminated due to -Wfatal-errors.


    Your program is incomplete. I guess it also contains some includes for
    cout and the using directive "using namespace std;". Somehow std::min
    from <algorithm> gets pulled in and due to your using directive you now
    have two function templates called min at the global level. So, your
    problem is of the form

    namespace foo {
    void bar();
    }

    void bar();

    using namespace foo;

    void test() {
    bar(); // ambiguous! which one do you mean? ::bar or foo::bar?
    }

    int main() {
    test();
    }

    Your options:
    (1) get rid of "using namespace std;"
    (2) fully qualify the function you want to call.
    (3) put your own code into your own namespace like this:

    namespace foo {
    void bar();
    }

    namespace me {
    void bar();

    using namespace foo;

    void test() {
    bar(); // not ambiguous anymore --> me::bar
    }
    }

    int main() {
    me::test();
    }

    Here, me::bar hides foo::bar because a using directive is like pulling
    all the names into the global scope but name lookup will first find
    me::bar and hence not search the global scope anymore.

    Cheers!
    SG
     
    SG, Mar 27, 2013
    #11
  12. Sooraj S

    MikeWhy Guest

    Sooraj S <> Wrote in message:
    > H, I am new to templates. Can anyone tell me why the program gives me
    > the error?
    >
    > template <class T> T min(T a, T b)
    > {
    > if (a < b) { cout << a << "is minimum"; }
    > else { cout << b << "is minimum"; }
    > return (a<b) ? a:b;
    > }
    > int main()
    > {
    > min <int> (10,12);
    > }
    >
    > OUTPUT:
    > In function 'int main()':
    > Line 9: error: call of overloaded 'min(int, int)' is ambiguous
    > compilation terminated due to -Wfatal-errors.
    >
    >


    Somewhere preceding that snippet is:
    using namespace std;

    search the web for discussions of why you should or shouldn't use it.
    --




    ----Android NewsGroup Reader----
    http://www.piaohong.tk/newsgroup
     
    MikeWhy, Apr 10, 2013
    #12
    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. dave
    Replies:
    3
    Views:
    2,405
    Mark Fitzpatrick
    Jan 19, 2006
  2. Venkat
    Replies:
    2
    Views:
    448
    Mark Preston
    Sep 15, 2003
  3. Hseu-Ming Chen
    Replies:
    1
    Views:
    1,016
    Chris Torek
    Jun 12, 2011
  4. Ravikanth
    Replies:
    2
    Views:
    486
    Ravikanth
    Jul 7, 2011
  5. Arthur Dzhelali

    Webservice Doesnot work.

    Arthur Dzhelali, Oct 19, 2006, in forum: ASP .Net Web Services
    Replies:
    0
    Views:
    150
    Arthur Dzhelali
    Oct 19, 2006
Loading...

Share This Page