A
asit
Why the following code shows terrific error ???
//minmax.h
template <class T>
T max(T a, T b)
{
if(a>b)
return a;
else
return b;
}
template <class T>
T min(T a, T b)
{
if(a>b)
return b;
else
return a;
}
//tminmax.cpp
#include <iostream>
#include "minmax.h"
using namespace std;
int main()
{
int i1=100, i2=200;
double d1=3.14159, d2=9.87654;
char c1='A', c2='z';
cout<<"max(i1, i2) == "<<max(i1, i2)<<endl;
cout<<"max(d1, d2) == "<<max(d1, d2)<<endl;
cout<<"max(c1, c2) == "<<max(c1, c2)<<endl;
cout<<"min(i1, i2) == "<<min(i1, i2)<<endl;
cout<<"min(d1, d2) == "<<min(d1, d2)<<endl;
cout<<"min(c1, c2) == "<<min(c1, c2)<<endl;
return 0;
}
When I compiled it, it gave me the following o/p
C:\cpp>g++ -o tminmax.exe tminmax.cpp
tminmax.cpp: In function `int main()':
tminmax.cpp:12: error: call of overloaded `max(int&, int&)' is
ambiguous
minmax.h:3: note: candidates are: T max(T, T) [with T = int]
F:/Dev-Cpp/bin/../lib/gcc/mingw32/3.4.2/../../../../include/c++/3.4.2/
bits/stl_a
lgobase.h:173: note: const _Tp& std::max(const _Tp&,
const _Tp&)
[with _Tp = int]
tminmax.cpp:13: error: call of overloaded `max(double&, double&)' is
ambiguous
minmax.h:3: note: candidates are: T max(T, T) [with T = double]
F:/Dev-Cpp/bin/../lib/gcc/mingw32/3.4.2/../../../../include/c++/3.4.2/
bits/stl_a
lgobase.h:173: note: const _Tp& std::max(const _Tp&,
const _Tp&)
[with _Tp = double]
tminmax.cpp:14: error: call of overloaded `max(char&, char&)' is
ambiguous
minmax.h:3: note: candidates are: T max(T, T) [with T = char]
F:/Dev-Cpp/bin/../lib/gcc/mingw32/3.4.2/../../../../include/c++/3.4.2/
bits/stl_a
lgobase.h:173: note: const _Tp& std::max(const _Tp&,
const _Tp&)
[with _Tp = char]
tminmax.cpp:16: error: call of overloaded `min(int&, int&)' is
ambiguous
minmax.h:12: note: candidates are: T min(T, T) [with T = int]
F:/Dev-Cpp/bin/../lib/gcc/mingw32/3.4.2/../../../../include/c++/3.4.2/
bits/stl_a
lgobase.h:151: note: const _Tp& std::min(const _Tp&,
const _Tp&)
[with _Tp = std::streamsize]
tminmax.cpp:17: error: call of overloaded `min(double&, double&)' is
ambiguous
minmax.h:12: note: candidates are: T min(T, T) [with T = double]
F:/Dev-Cpp/bin/../lib/gcc/mingw32/3.4.2/../../../../include/c++/3.4.2/
bits/stl_a
lgobase.h:151: note: const _Tp& std::min(const _Tp&,
const _Tp&)
[with _Tp = double]
tminmax.cpp:18: error: call of overloaded `min(char&, char&)' is
ambiguous
minmax.h:12: note: candidates are: T min(T, T) [with T = char]
F:/Dev-Cpp/bin/../lib/gcc/mingw32/3.4.2/../../../../include/c++/3.4.2/
bits/stl_a
lgobase.h:151: note: const _Tp& std::min(const _Tp&,
const _Tp&)
[with _Tp = char]
//minmax.h
template <class T>
T max(T a, T b)
{
if(a>b)
return a;
else
return b;
}
template <class T>
T min(T a, T b)
{
if(a>b)
return b;
else
return a;
}
//tminmax.cpp
#include <iostream>
#include "minmax.h"
using namespace std;
int main()
{
int i1=100, i2=200;
double d1=3.14159, d2=9.87654;
char c1='A', c2='z';
cout<<"max(i1, i2) == "<<max(i1, i2)<<endl;
cout<<"max(d1, d2) == "<<max(d1, d2)<<endl;
cout<<"max(c1, c2) == "<<max(c1, c2)<<endl;
cout<<"min(i1, i2) == "<<min(i1, i2)<<endl;
cout<<"min(d1, d2) == "<<min(d1, d2)<<endl;
cout<<"min(c1, c2) == "<<min(c1, c2)<<endl;
return 0;
}
When I compiled it, it gave me the following o/p
C:\cpp>g++ -o tminmax.exe tminmax.cpp
tminmax.cpp: In function `int main()':
tminmax.cpp:12: error: call of overloaded `max(int&, int&)' is
ambiguous
minmax.h:3: note: candidates are: T max(T, T) [with T = int]
F:/Dev-Cpp/bin/../lib/gcc/mingw32/3.4.2/../../../../include/c++/3.4.2/
bits/stl_a
lgobase.h:173: note: const _Tp& std::max(const _Tp&,
const _Tp&)
[with _Tp = int]
tminmax.cpp:13: error: call of overloaded `max(double&, double&)' is
ambiguous
minmax.h:3: note: candidates are: T max(T, T) [with T = double]
F:/Dev-Cpp/bin/../lib/gcc/mingw32/3.4.2/../../../../include/c++/3.4.2/
bits/stl_a
lgobase.h:173: note: const _Tp& std::max(const _Tp&,
const _Tp&)
[with _Tp = double]
tminmax.cpp:14: error: call of overloaded `max(char&, char&)' is
ambiguous
minmax.h:3: note: candidates are: T max(T, T) [with T = char]
F:/Dev-Cpp/bin/../lib/gcc/mingw32/3.4.2/../../../../include/c++/3.4.2/
bits/stl_a
lgobase.h:173: note: const _Tp& std::max(const _Tp&,
const _Tp&)
[with _Tp = char]
tminmax.cpp:16: error: call of overloaded `min(int&, int&)' is
ambiguous
minmax.h:12: note: candidates are: T min(T, T) [with T = int]
F:/Dev-Cpp/bin/../lib/gcc/mingw32/3.4.2/../../../../include/c++/3.4.2/
bits/stl_a
lgobase.h:151: note: const _Tp& std::min(const _Tp&,
const _Tp&)
[with _Tp = std::streamsize]
tminmax.cpp:17: error: call of overloaded `min(double&, double&)' is
ambiguous
minmax.h:12: note: candidates are: T min(T, T) [with T = double]
F:/Dev-Cpp/bin/../lib/gcc/mingw32/3.4.2/../../../../include/c++/3.4.2/
bits/stl_a
lgobase.h:151: note: const _Tp& std::min(const _Tp&,
const _Tp&)
[with _Tp = double]
tminmax.cpp:18: error: call of overloaded `min(char&, char&)' is
ambiguous
minmax.h:12: note: candidates are: T min(T, T) [with T = char]
F:/Dev-Cpp/bin/../lib/gcc/mingw32/3.4.2/../../../../include/c++/3.4.2/
bits/stl_a
lgobase.h:151: note: const _Tp& std::min(const _Tp&,
const _Tp&)
[with _Tp = char]