Qt5.3编译错误——call of overloaded ‘max(int int)’is ambiguous
2015-04-27 20:56
344 查看
错误描述:
今天在使用Qt写一个C++函数模板的测试程序的时候,编译的时候,编译的时候出现如下错误:错误描述为:在main函数中,进行函数max()重载时,出现(ambiguous)含糊的,不明确的;引起歧义的使用;
因为第一次遇到这种错误,写篇内容纪念一下吧。
测试代码如下:
#include <iostream> using namespace std; template <typename T>//typename == class T max(T a,T b) { return a>b?a:b; } int main() { int a=1,b=2; cout<<max(a,b)<<endl; cout<<max(1,2)<<endl; cout<<max(2,3)<<endl; cout<<max(1.1f,2.2f)<<endl; cout<<max(1.11l,2.22l)<<endl; cout<<max('A','C')<<endl; //上面都是隐式的调用int,float.double.char类型的,而下面必须显示的调用, //因为1是int类型的,而2.0是double类型的,此时,如果不明确指定数据类型,编译器无法识别。 cout<<max<int>(1,2.0)<<endl; }
单从代码看,我看不出什么错误,而且我记得当初学习C++模板的时候,也是这样写的,只不过,当初使用的IDE是VC6.0,难道环境不同。所以我把代码贴到VC上,测试一下,完美的成功运行了。看来真是IDE不同的问题。但对于Qt,应该有解决办法的,上网找了一下,原来是因为函数名冲突的问题,这里我先说下解决方法。
解决方法:
调用时声明名字空间,把max(a,b);改成::max(a,b);前加::,避免冲突改变函数名称,把自定义的函数max改成myMax或其它。
造成问题的原因:
max函数与标准库的std::max函数冲突。看来Qt中已经有了标准的max()的函数了,我测试了一下,还真有这个函数。
问题深究:
从这个错误中,我们知道自己写的函数名容易和标准库或其他文件里的函数冲突,那么C++是怎么处理函数名冲突的呢?这就用到了命名空间。名称空间是一项c++de 特性,是用来解决在编写大型程序中不同文件(厂商)中相同变量名问题。 例如:就像我编写的函数max和标准的max,为了能够准确调用其中一个max()函数,我们将这两个函数分别放在不同在命名空间单元中。这样就可以用 "命名空间::函数名" 的方式准确调用所需函数。
::max();//调用的自己写的max()
std::max();//调用的标准模板库的max
我们经常会使用
using namespace std;其中的std是c++在标准命名空间。
要让程序访问std有以下四种方法:
将using namespace std;放在函数定义之前,让文件中所有的函数都能够使用命名空间std中在元素。
将using namespace std;放在特定在函数定义中,让该函数能够使用命名空间std中元素。
在特定函数中使用using std::cout; 这样的编译指令,让该函数使用命名空间std中所指定在元素,如 cout。这种方法在好处是,防止自己定义的变量名与std中在元素名重复。
完全不使用编译指令using,在需要使用std中元素时,使用前缀std::。
通过上面的第4条,这个问题的解决方法还有一种,就是不使用
using namespace std;,但是cout,cin等一些标准的库函数就不能使用了,所以最好的办法是修改函数名。
相关文章推荐
- 问题:QT mplayer 编译错误call of overloaded 'number(HWND_*)' is ambiguous
- C++: 解释error: call of overloaded ‘abs(int)’ is ambiguous
- call of overloaded 'max(char&, char&)' is ambiguous
- Qt error ------ no matching function for call to QObject::connect(QSpinBox*&, <unresolved overloaded function type>, QSlider*&, void (QAbstractSlider::*)(int))
- G++编译错误 no matching function for call to '_tag_data_value::StringAddLen(std::string, int, int)
- QT + VS2005 编译出现 error LNK2001: 无法解析的外部符号 "public: virtual struct QMetaObject const * __thiscall Widget::metaObject 错误
- QT:编译错误:ISO C++ forbids declaration of *** with no type
- std::numeric_limits<unsigned int>::max()编译错误
- Qt编译错误: 'UnicodeUTF8' is not a member of 'QApplication'
- Qt 编译错误 提示TypeError: Property 'asciify' of object Core::Internal::UtilsJsExtension(0x27a9278) is not
- QT 编译错误 multiple definition of
- Qt编译错误之out of memory allocating 1073745919 bytes
- Qt 编译时出现“-1: error: LNK2019: unresolved external symbol "public: void __thiscall M”错误
- ubuntu64位下交叉编译opencv3.3遇到的make[error]error: call of overloaded 'abs(long long int&)' is ambiguous
- VC错误:编译程序中出现“fatal error C1010: unexpected end of file”
- Qt5.7中编译报错:“invalid use of incomplete type 'class Ui::xxx'ui(new Ui::xxx)”的解决方法
- 遇到错误 org.hibernate.HibernateException: identifier of an instance of XXX was altered from X to X 将int
- 两个常见Qt编译错误的解决
- QT 之 编译错误(三)
- c++编译错误:invalid new-expression of abstract class type