c++ error 2064: term does not evaluate to a function taking 1 arguments解决方法
来源:互联网 发布:80端口被4和0占用 编辑:程序博客网 时间:2024/06/06 15:57
首先来看一个简单的示例:
class T{public: T() = default; ~T() = default; int convertToInt(double x) { return ceil(x); } void doSomething() { vector<double> vecD{ 2.5, 3.0, 4.9 }; vector<int> vecI; std::transform(vecD.begin(), vecD.end(), std::back_inserter(vecI), mem_fn(&T::convertToInt)); }};
这个端代码编译时会报错“c++ error 2064: term does not evaluate to a function taking 1 arguments”,这个错误是在哪条语句报出来的呢?是在tranform这个函数中调用的_transform方法:
template<class _InIt, class _OutIt, class _Fn1> inline _OutIt _Transform(_InIt _First, _InIt _Last, _OutIt _Dest, _Fn1 _Func) { // transform [_First, _Last) with _Func for (; _First != _Last; ++_First, ++_Dest) *_Dest = _Func(*_First); //就是这一行 return (_Dest); }
编译器抱怨的是“_Func这个函数不能转为接受一个参数的函数”,但以上代码使用的函数“converToInt”明明是只接受一个参数的函数,那为什么编译器还会报这个错呢?这是因为在这个例子中,mem_fn返回一个仿函数,而这个仿函数接受两个参数,一个是T*指针,第二个才是我们需要的double。所以编译器会产生这个错误。
为了解决这个错误,有以下几种方法:
1. 将transfomr那行改为:
std::transform(vecD.begin(), vecD.end(), std::back_inserter(vecI), std::bind1st(std::mem_fn(&T::convertToInt),this));
2. 将函数converToInt改为一个静态方法:
static int convertToInt(double x) {return ceil(x);}
这样一来,由于static方法没有this指针,所以mem_fn返回的是一个值接受一个double作为参数的仿函数。
3. C++ 11之后,使用lambda表达式
将tranform那个函数调用改为:
std::transform(vecD.begin(), vecD.end(), std::back_inserter(vecI), [this](double & dVal) { return this->convertToInt(dVal); });
0 0
- c++ error 2064: term does not evaluate to a function taking 1 arguments解决方法
- error C2064: term does not evaluate to a function
- C2064: term does not evaluate to a function解决方法
- VC6.0: error C2064: term does not evaluate to a function
- 类成员指针 error C2064: term does not evaluate to a function
- VC中出现error C2064: term does not evaluate to a function
- error C2064: term does not evaluate to a function惯性思维:乘号写掉了的坏习惯
- error C2660: 'wcscpy_s' : function does not take 2 arguments
- error C2660: 'CWnd::HtmlHelpW ' : function does not take 4 arguments
- .size expression for apf_page_fault does not evaluate to a constant
- size expression for do_hypervisor_callback does not evaluate to a constant
- size expression for do_hypervisor_callback does not evaluate to a constant
- 手动编译memcached时遇到memcache.c error: too few arguments to function ‘zend_list_insert’的解决方法
- 'MessageBox' : function does not take 3 arguments
- fuse_trash.c:119: error: too few arguments to function 'hdfsDelete'
- Android bdk-build error:libxxx.so/.a use VFP register arguments, output does not解决办法
- vmware 共享文件夹配置及 error: too many arguments to function ‘wait_on_bit’ 解决方法
- c++引用标准c库close出错 error: too many arguments to function call, expected 0, have 1
- 跟我一起写 Makefile(八)
- 博客备份
- 跟我一起写 Makefile(九)
- 跟我一起写 Makefile(十)
- Swift基础语法--字符串
- c++ error 2064: term does not evaluate to a function taking 1 arguments解决方法
- 【Leetcode】Lowest Common Ancestor of a Binary Tree
- 跟我一起写 Makefile(十一)
- NYOJ 64 鸡兔同笼
- batlite
- 前端上传组件Plupload使用指南
- 跟我一起写 Makefile(十二)
- 跟我一起写 Makefile(十三)
- 053-3 While tuning a SQLstatement, the SQL Tuning Advisor finds an existing SQL profile for