call of overloaded 'max(char&, char&)' is ambiguous
来源:互联网 发布:销售数据分析范文 编辑:程序博客网 时间:2024/05/16 12:34
最近准备着考试,其中遇到了一个题目,具体程序如下:
#include <iostream>#include <conio.h>using namespace std;#define SAFEDELETE(p) if(p){delete p;p=NULL;}template<typename T>T max(T x, T y){ cout<<"This is a template func!"<<endl; return (x>y)? x:y;}int max(int x, int y){ cout<<"int int"<<endl; return (x>y)?x:y;}int max(int x, char y){ cout<<"int char"<<endl; return (x>y)?x:y;}int main(void){ int i=10; float f = 3; char c = 'c'; max(i,i); max(c,c); max(i,c); max(c,i); max(f,f); return 0;}首先说问题1:
这个程序在vc6上面是可以通过编译的,而在vc2008和codeblocks是通不过编译的,错误提示是:
call of overloaded 'max(char&, char&)' is ambiguous
其实,原因很简单,因为使用using namespace std所造成的,因为std里面有max(char&, char&)这个函数,所以,会和它造成二义性,也就通不过编译了
所以,改成不使用std namespace然后使用完整的std::就可以了
再来说问题2:
关于重载函数的匹配问题,vc6上面的,第四个结果是int char,而其他编译器是int int的结果,其实后者才是准确的,具体原因如下:
确定最佳可行函数的步骤,首先应确定哪些是完全匹配的,完全匹配从最佳到最差的顺序为
a、完全匹配,但普通函数优于模板函数及模板函数的特化版本
b、提升转换(如:char 和short 转换为 int,及float 转换为 double)
c、标准转换(如:int 转换为 char,及long 转换为 double)
d、用户定义的转换,如类声明中定义的 转换
更为具体的,可以参见百度文库里面的文档:
http://wenku.baidu.com/link?url=EqzXNog4ML3WiXskHdfhUCW35cKG5TwgH8lp_33jw6h354MFqOGvJSF3za3vbCC_yby4-19KfThr8TGD3InrRK34lLEWDQDMzv_StSPYyem
0 0
- call of overloaded 'max(char&, char&)' is ambiguous
- 平日小问题 (1): call of overloaded 'max(char&, char&)' is ambiguous
- call of overloaded 'round(&float)' is ambiguous
- call of overloaded `pow(int, int)' is ambiguous;
- C++: 解释error: call of overloaded ‘abs(int)’ is ambiguous
- Bug-List: call of overloaded 'sqrt(u16&)' is ambiguous
- 问题:QT mplayer 编译错误call of overloaded 'number(HWND_*)' is ambiguous
- cocos2dx 3.2在Eclipse上打包出现error: call of overloaded 'abs(float)' is ambiguous
- ambiguous call to overloaded function
- json error: Use of overloaded operator [] is ambiguous
- ubuntu64位下交叉编译opencv3.3遇到的make[error]error: call of overloaded 'abs(long long int&)' is ambiguous
- 'sqtr'ambiguous call to overloaded function
- error C2668: 'sqrt' : ambiguous call to overloaded
- error C2668: 'abs' : ambiguous call to overloaded function
- ambiguous call to overloaded function解决的办法
- C2668: 'sqrt' : ambiguous call to overloaded function(sqrt问题)
- 【关于DrawLine' : ambiguous call to overloaded function解决办法】
- error C2668: ‘pow’ : ambiguous call to overloaded function
- Poj 1665
- (经典)使用group by出现错误.要注意什么?
- nuget.exe install -source 已退出,代码为 3 求解决方案
- IOS高访新浪微博界面(讲解如何自定义UITableViewCell,处理@#链接 特殊字符)
- Android动画学习笔记-Android Animation
- call of overloaded 'max(char&, char&)' is ambiguous
- u-boot实现USB下载功能(2)
- 小认识句柄
- android显示view时各回调函数执行顺序
- 软件架构之我见
- flex 上拉页面显示datagrid
- Linux程序调试GDB——数据查看
- ASIHTTPRequest实现断点下载
- nosql知识1