template program doesnot work

S

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

Gert-Jan de Vos

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
 
S

Stefan Ram

Sooraj S said:
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?
 
8

88888 Dihedral

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

Ian Collins

Sooraj said:
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.
 
H

Haochen Xie

Sooraj said:
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.
 
8

88888 Dihedral

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

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

Ian Collins

88888 said:
Haochen Xieæ–¼ 2013å¹´3月27日星期三UTC+8下åˆ12時42分39秒寫é“:

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

SG

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
 
M

MikeWhy

Sooraj S said:
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
 

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

Similar Threads


Members online

Forum statistics

Threads
473,982
Messages
2,570,189
Members
46,734
Latest member
manin

Latest Threads

Top