RCPP - 调用'transform'没有匹配功能

RCPP - no matching function for call to 'transform'

本文关键字:功能 RCPP transform 调用      更新时间:2023-10-16

我在Mac上使用RCPP时有问题(在Windows上没有发生问题(。

这是导致错误的C 代码。

#include <Rcpp.h>
using namespace Rcpp;
NumericVector vecpow(const IntegerVector base, const NumericVector exp) 
{
  NumericVector out(base.size());
  std::transform(base.begin(), base.end(), exp.begin(), out.begin(), ::pow);
  return out;
}

似乎没有什么太花哨或复杂的。

尝试编译时,我仍然会遇到以下错误:

na_ma.cpp:7:3:错误:呼叫"转换"的匹配功能无 std :: transform(base.begin((,base.end((,exp.begin((,out.begin((,:: pow(; ^~~~~~~~~~~~~~

/library/developer/commandlinetools/usr/include/c /v1/算法:2028:1:注意:候选功能模板不可行:需要4个参数,但提供了5个参数 变换(_inputiterator __first,_inputiterator __last,_outputiterator __ result,_unaryoperation __OP( ^

我想知道如何解决此问题。在搜索解决方案时,我提出了一些建议创建makevars文件的建议 - 但这对我不起作用。

如果有人可以向我解释,为什么我不了解此错误,为什么会发生此错误。

这实际上是C 编译器错误。编译器无法与BinaryOp匹配:: POW,因此将其包装到Lambda中。这对我有用

std::transform(base.cbegin(), base.cend(), exp.cbegin(), out.begin(), [](double a, double b) {return ::pow(a, b); });

如果lambdas不可用,可以尝试制作一个函子(Lambda等同于,请检查https://medium.com/@winwardo/c-lambdas-arent-magic-magic-part-part-part-part-1-b56df2d92d2ad2,https://medium.com/@winwardo/c-lambdas-arent-magic-part-part-2-ce0b48934809(。沿线(未经测试的代码,我不在计算机上(

struct pow_wrapper {
    public: double operator()(double a, double b) {
        return ::pow(a, b);
    }
};

然后尝试

std::transform(base.cbegin(), base.cend(), exp.cbegin(), out.begin(), pow_wrapper());