The call to the overloaded 'min (int &, int &)' is ambiguous

I have a problem with the template. This code passed under vc6 but did not pass under g ++. Can someone explain the reason to me? thank.

#include<iostream>
using namespace std;

template<class T>
T min(T x, T y) {
    return (x < y ? x : y);
}

int main() {
    int i1 = 23, i2 = 15, i;
    float f1 = 23.04, f2 = 43.2, f;
    double d1 = 0.421342, d2 = 1.24342343, d;
    i = min(i1, i2);
    f = min(f1, f2);
    d = min(d1, d2);
    cout << "The smaller of " << i1 << " and " << i2 << " is " << i << endl;
    cout << "The smaller of " << f1 << " and " << f2 << " is " << f << endl;
    cout << "The smaller of " << d1 << " and " << d2 << " is " << d << endl;
}

"/usr/bin/make" -f nbproject/Makefile-Debug.mk QMAKE = SUBPROJECTS =.build-conf "/usr/bin/make" -f nbproject/Makefile-Debug.mk dist/Debug/GNU-MacOSX/traincpp mkdir -p build/Debug/GNU-MacOSX rm -f build/Debug/GNU-MacOSX/newmain.o.d g++ -c -g -MMD -MP -MF build/Debug/GNU-MacOSX/newmain.o.d -o /Debug/GNU -MacOSX/newmain.o newmain.cpp newmain.cpp: 'int main()': newmain.cpp: 13: : 'min (int &, int &)' newmain.cpp: 5: note: : T min (T, T) [ T = INT] /usr/include/c ++/4.2.1/bits/stl_algobase.h:182: : const _Tp & std:: min (const _Tp &, const _Tp &) [ _Tp = int] newmain.cpp: 14: : 'min (float &, float &)' newmain.cpp: 5: : : T min (T, T) [ T = ] /usr/include/c ++/4.2.1/bits/stl_algobase.h:182: : const _Tp & std:: min (const _Tp &, const _Tp &) [ _Tp = float] newmain.cpp: 15: : 'min (double &, double &) ' newmain.cpp: 5: : : T min (T, T) [ T = double] /usr/include/c ++/4.2.1/bits/stl_algobase.h:182: : const _Tp & std:: min (const _Tp &, const _Tp &) [ _Tp = double] make [2]: * [//GNU-MacOSX/newmain.o] 1 [1]: [.build-conf] 2: ** [.build-impl] 2

生成 失败 (退出 值 2, 总计 时间: 623 毫秒)

+3
5

, std, -. , std::min. using namespace std; :

using std::cout;
using std::endl;

:

std::cout << "The smaller of " << i1 << " and " << i2 << " is " << i << std::endl;
+11

, min() g++.

+1

iostream include min, , (- using namespace) . min .

+1

, std:: cout, std min , , abc... , min, abc:: min.. this .

+1

The swap () function already exists in iostream. Therefore, it conflicts with your swap () function. You may need to specify which swap () you want to use or change the name of your swap function, e.g. swap1 (), swap2 (), etc. You can change any letter in UPPERCASE of your swap function, for example Swap () , this can solve the problem without deletingusing namespace std;

otherwise just delete using namespace std'and enter

using std :: cout;
using std :: endl;

instead of OR write code as -

std :: cout << "After swapping - a = " << a << ", b = " << b << std :: endl;

What is it. Thank.

-1
source

All Articles