Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > template program doesnot work

Reply
Thread Tools

template program doesnot work

 
 
Sooraj S
Guest
Posts: n/a
 
      03-26-2013
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.

 
Reply With Quote
 
 
 
 
Gert-Jan de Vos
Guest
Posts: n/a
 
      03-26-2013
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
 
Reply With Quote
 
 
 
 
Stefan Ram
Guest
Posts: n/a
 
      03-26-2013
Sooraj S <(E-Mail Removed)> 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);
>}


 
Reply With Quote
 
88888 Dihedral
Guest
Posts: n/a
 
      03-26-2013
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.
 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      03-26-2013
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
 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      03-26-2013
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
 
Reply With Quote
 
Haochen Xie
Guest
Posts: n/a
 
      03-27-2013
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
 
Reply With Quote
 
Haochen Xie
Guest
Posts: n/a
 
      03-27-2013
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
 
Reply With Quote
 
88888 Dihedral
Guest
Posts: n/a
 
      03-27-2013
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.

 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      03-27-2013
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
multiple calls to show doesnot work for matplotlib Ravikanth Python 2 07-06-2011 11:31 PM
parallel computations: subprocess.Popen(...).communicate()[0] doesnot work with multiprocessing.Pool Hseu-Ming Chen Python 1 06-12-2011 10:00 PM
Webservice Doesnot work. Arthur Dzhelali ASP .Net Web Services 0 10-19-2006 12:38 PM
Microsoft.Web.UI.WebControls doesnot work when upladed to web hosting server dave ASP .Net 3 01-19-2006 03:07 AM
My HTML doesnot work in Netscape Venkat XML 2 09-15-2003 12:13 PM



Advertisments