重载的“ min(int&amp ;, int&)”的调用不明确 [英] call of overloaded 'min(int&, int&)' is ambiguous

查看:119
本文介绍了重载的“ min(int&amp ;, int&)”的调用不明确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在模板上遇到问题。此代码在vc6下通过,但在g ++下失败。
有人能告诉我原因吗?

I got some problem on template.This code passed under vc6 but failed under g++. Is there anybody could tell me the reason? thanks.

#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.od g ++
-c -g- MMD -MP -MF build / Debug / GNU-MacOSX / newmain.od -o
build / Debug / GNU-MacOSX / newmain.o
newmain.cpp newmain.cpp:在函数
中'int main()':newmain.cpp:13:错误:
重载的'min(int& ;, int&)'
是模糊的newmain.cpp:5:注意:
候选者是:T min(T,T)[with T =
int]
/usr/include/c++/4.2.1/bits/stl_algobase.h:182:
注意:const _Tp&
std :: min(const _Tp& ;, const _Tp&)[with
_Tp = int] newmain.cpp:14:错误:重载的min(float& ;, float&)的调用是
不明确的newmain.cpp:5:注意:
候选者为:T min(T,T)[T =
float]
/usr/include/c++/4.2.1 /bits/stl_algobase.h:182:
注意:const _Tp&
std :: min(const _Tp& ;, const _Tp&)[with
_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&)[with
_Tp = double] make [2]: * [build / Debug / GNU-MacOSX / newmain.o]
错误1 make [1]:
[.build-conf]
错误2 make:*
* [.build-impl]错误
2

"/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 build/Debug/GNU-MacOSX/newmain.o newmain.cpp newmain.cpp: In function 'int main()': newmain.cpp:13: error: call of overloaded 'min(int&, int&)' is ambiguous newmain.cpp:5: note: candidates are: T min(T, T) [with T = int] /usr/include/c++/4.2.1/bits/stl_algobase.h:182: note: const _Tp& std::min(const _Tp&, const _Tp&) [with _Tp = int] newmain.cpp:14: error: call of overloaded 'min(float&, float&)' is ambiguous newmain.cpp:5: note: candidates are: T min(T, T) [with T = float] /usr/include/c++/4.2.1/bits/stl_algobase.h:182: note: const _Tp& std::min(const _Tp&, const _Tp&) [with _Tp = float] newmain.cpp:15: error: call of overloaded 'min(double&, double&)' is ambiguous newmain.cpp:5: note: candidates are: T min(T, T) [with T = double] /usr/include/c++/4.2.1/bits/stl_algobase.h:182: note: const _Tp& std::min(const _Tp&, const _Tp&) [with _Tp = double] make[2]: * [build/Debug/GNU-MacOSX/newmain.o] Error 1 make[1]: [.build-conf] Error 2 make: ** [.build-impl] Error 2

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

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


推荐答案

这是因为您已导入所有 std 名称空间,它是一个不可以。请注意,其他候选者是模板 std :: min 。删除 using名称空间std; 并导入选择符号:

It's because you've imported all of the std namespace, which is a no-no. Note the other candidates are template std::min. Remove the using namespace std; and either import select symbols:

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

或使它们合格:

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

这篇关于重载的“ min(int&amp ;, int&amp;)”的调用不明确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆