call of overloaded 'xxx' is ambiguous
这里定义了一个模版函数,功能同STL里的copy函数:
#include <vector>
#include <list>
#include <iostream>template <typename Iter1, typename Iter2>
Iter2 copy(Iter1 f1, Iter1 e1, Iter2 f2)
{for (f1; f1 != e1; ++f1, ++f2){*f1 = *f2;}return f2;
}int main()
{int arr[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};std::vector<int> vec{1, 2, 3, 4, 5, 6, 7, 8, 9, 0};copy(arr, arr + 10, vec.begin());return 0;
}
编译时报了call of overloaded 'copy(int [10], int*, std::vector<int>::iterator)' is ambiguous
错误。
c:\Source\drill.cpp: In function 'int main()':
c:\Source\drill.cpp:27:36: error: call of overloaded 'copy(int [10], int*, std::vector<int>::iterator)' is ambiguouscopy(arr, arr + 10, vec.begin());^
c:\Source\drill.cpp:6:7: note: candidate: Iter2 copy(Iter1, Iter1, Iter2) [with Iter1 = int*; Iter2 = __gnu_cxx::__normal_iterator<int*, std::vector<int> >]Iter2 copy(Iter1 f1, Iter1 e1, Iter2 f2)^~~~
In file included from C:/mingw-w64/lib/gcc/x86_64-w64-mingw32/6.3.0/include/c++/vector:60:0,from c:\Source\drill.cpp:1:
C:/mingw-w64/lib/gcc/x86_64-w64-mingw32/6.3.0/include/c++/bits/stl_algobase.h:446:5: note: candidate: _OI std::copy(_II, _II, _OI) [with _II = int*; _OI = __gnu_cxx::__normal_iterator<int*, std::vector<int> >]copy(_II __first, _II __last, _OI __result)
从报错信息可以看出,编译器匹配到了stl_algobase.h
里的copy函数,因为我传的参数std::vector::iterator(vec.begin())
是属于namespace std
的。
解决的办法一:指定命名空间
#include <vector>
#include <list>
#include <iostream>
namespace LK
{template <typename Iter1, typename Iter2>Iter2 copy(Iter1 f1, Iter1 e1, Iter2 f2){for (f1; f1 != e1; ++f1, ++f2){*f1 = *f2;}return f2;}
}int main()
{int arr[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};std::vector<int> vec{1, 2, 3, 4, 5, 6, 7, 8, 9, 0};LK::copy(arr, arr + 10, vec.begin());return 0;
}
解决的办法二:函数名用括号包住
#include <vector>
#include <list>
#include <iostream>template <typename Iter1, typename Iter2>
Iter2 copy(Iter1 f1, Iter1 e1, Iter2 f2)
{for (f1; f1 != e1; ++f1, ++f2){*f1 = *f2;}return f2;
}int main()
{int arr[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};std::vector<int> vec{1, 2, 3, 4, 5, 6, 7, 8, 9, 0};(copy)(arr, arr + 10, vec.begin());return 0;
}
参考资料
- template programming: ambiguous call to overloaded function
- Argument-dependent name lookup
- Argument-dependent lookup
转载于:https://www.cnblogs.com/lkpp/p/argument-dependent-name-koenig-lookup-on-functions.html
call of overloaded 'xxx' is ambiguous相关推荐
- Call of Overloaded Function is ambiguous
error: Call of Overloaded Function is ambiguous 看例子吧 #include<iostream>using namespace std;int ...
- error: reference to ‘XXX’ is ambiguous
error: reference to 'DataType' is ambiguousICudaEngine* createEngine(unsigned int maxBatchSize, IBui ...
- git-svn:通过git来管理svn代码
简介 svn和git都是常用的版本管理软件,但是git无论在理念或是功能上都比svn更为先进.但是有的公司是以svn作为中央仓库,这时git与svn代码的同步就可以通过 git-svn这个软件进行,从 ...
- 错误:请求“ ..”中的成员“ ..”属于非类类型
我有一个带有两个构造函数的类,一个不带参数,而一个带一个参数. 使用接受一个参数的构造函数创建对象的工作符合预期. 但是,如果我使用不带参数的构造函数创建对象,则会出现错误. 例如,如果我编译此代码( ...
- c++ -- 构造函数与析构函数
一.构造函数的介绍 1. 构造函数的作用 构造函数主要用来在创建对象时完成对对象属性的一些初始化等操作, 当创建对象时, 对象会自动调用它的构造函数.一般来说, 构造函数有以下三个 ...
- 来看一下:构造函数的小秘密详解
**************************************************构造函数********************************************** ...
- 使用Git管理SVN
简介 当前,大多数开发中的开源项目以及大量的商业项目都使用 Subversion 来管理源码.作为最流行的开源版本控制系统,Subversion 已经存在了接近十年的时间.它在许多方面与 CVS 十分 ...
- conversion function
文章目录 conversion function (自定义类型转换为其余类型) non-explicit-one-argument constructor (基本类型转换为自定义类型) convers ...
- Java包与Import导入
包的概念 包是Java语言提供的一种确保类名唯一性的机制,是类的一种组织和管理方式.是一组功能相似或相关的类或接口的集合. 一个完整的类名是包名+类名,在没有import导入的情况下,使用一个类需要给 ...
最新文章
- linux 逆向工具 radare2入门
- LVS工作总结之原理篇–DR模式
- LeetCode 1058. 最小化舍入误差以满足目标(排序+贪心)
- 计算android view的FPS,使用Android SurfaceView的低FPS
- Android桌面悬浮窗仿QQ手机管家加速效果
- 从键盘读取数据,回车才能显示的问题
- Oracle redo 日志切换时间频率
- ecmall挂件开发实例二(转)
- 【已解决】微信小程序web-view嵌套H5,使用setStorageSync,localStorage.setItem有时候失效的问题
- 小米手机miui12系统下拉通知栏有通知但是看不到通知内容
- 数据库系统概论 (王珊.第五版)第七章数据库设计习题答案
- learn-motrix and 百度网盘
- 2的n次方对照表,最大256次方,2021年
- Nvidia TX2 使用Intelrealsense L515 并安装 RTABmap建图包编译成功
- 各国语言名称英文简写对照表
- jstack分析 (Thread state=IN_NATIVE) mark
- Python编程:从入门到实践------第6章:字典
- python 3.10不支持torch,因而无法安装和使用cnocr
- 【产品经理三节课】第3章 需求挖掘入门
- 吉首大学2019年程序设计竞赛(重现赛) J 滑稽树下你和我 (递归)