Rcpp 'candidate function has different number of parameters (expected 0 but has 1)'

Rcpp 'candidate function has different number of parameters (expected 0 but has 1)'

我对 C++ 和 Rcpp 还很陌生。我有以下函数,它将包含大整数的字符串表示形式的字符向量转换为整数,然后将它们转换为十六进制数并获取生成的十六进制数的子字符串。我可以将此函数与 sourceCpp 一起使用,它会生成预期的结果。

#include <Rcpp.h>
#include <iostream>
#include <ostream>
using namespace Rcpp;
// [[Rcpp::export]]
Rcpp::StringVector mcc(Rcpp::StringVector  x) {
  unsigned short int n = x.size();
  Rcpp::StringVector output(n);
  for(int i = 0; i < n; ++i) {
    unsigned long int number = std::strtoul(x[i], NULL, 10);
    std::stringstream sstream;
    sstream << std::hex << number;
    std::string result = sstream.str();
    output[i] = result.substr(0,3);
    sstream.str("");
  }
  return output;
}

但是,当我尝试编译该函数以将其包含在我正在构建的包中时,编译器(clang++,在 Mac 上)抛出以下错误:

RcppExports.cpp:24:40: error: address of overloaded function 'mcc' does not match required type 'void *()'
{"mcc",                 (DL_FUNC) &mcc,                 1},
                                   ^~~
RcppExports.cpp:20:17: note: candidate function has different number of parameters (expected 0 but has 1)
RcppExport SEXP mcc(SEXP);
                ^
RcppExports.cpp:9:20: note: candidate function has different number of parameters (expected 0 but has 1)
Rcpp::StringVector mcc(Rcpp::StringVector x);

我几乎没有使用 C++ 的经验,所以我什至不明白它在这里说的是什么。 google/Whosebug 搜索错误消息没有 return 任何有用的结果。

感谢您的帮助!

--- 编辑 ----

这是 compileAttributes() 生成的代码(运行 两次)

// Generated by using Rcpp::compileAttributes() -> do not edit by hand
// Generator token: 10BE3573-1514-4C36-9D1C-5A225CD40393

#include <Rcpp.h>

using namespace Rcpp;

// mcc
Rcpp::StringVector mcc(Rcpp::StringVector x);
RcppExport SEXP _cropMobileData_mcc(SEXP xSEXP) {
BEGIN_RCPP
    Rcpp::RObject rcpp_result_gen;
    Rcpp::RNGScope rcpp_rngScope_gen;
    Rcpp::traits::input_parameter< Rcpp::StringVector >::type x(xSEXP);
    rcpp_result_gen = Rcpp::wrap(mcc(x));
    return rcpp_result_gen;
END_RCPP
}

RcppExport SEXP mcc(SEXP);

static const R_CallMethodDef CallEntries[] = {
    {"_cropMobileData_mcc", (DL_FUNC) &_cropMobileData_mcc, 1},
    {"mcc",                 (DL_FUNC) &mcc,                 1},
    {NULL, NULL, 0}
};

RcppExport void R_init_cropMobileData(DllInfo *dll) {
    R_registerRoutines(dll, NULL, CallEntries, NULL, NULL);
    R_useDynamicSymbols(dll, FALSE);
}

您的代码非常好。 Rcpp 非常好。 R 完全没问题。

但是您系统上的 某些东西 不是,我们无法远程判断是什么问题。

完整日志如下。

首先我们创建一个包

edd@brad:/tmp$ Rscript -e 'Rcpp::Rcpp.package.skeleton("jeroenclaes")'
Creating directories ...
Creating DESCRIPTION ...
Creating NAMESPACE ...
Creating Read-and-delete-me ...
Saving functions and data ...
Making help files ...
Done.
Further steps are described in './jeroenclaes/Read-and-delete-me'.

Adding Rcpp settings
 >> added Imports: Rcpp
 >> added LinkingTo: Rcpp
 >> added useDynLib directive to NAMESPACE
 >> added importFrom(Rcpp, evalCpp) directive to NAMESPACE
 >> added example src file using Rcpp attributes
 >> added Rd file for rcpp_hello_world
 >> compiled Rcpp attributes
edd@brad:/tmp$ cd jeroenclaes/

接下来我们复制你的代码

edd@brad:/tmp/jeroenclaes$
edd@brad:/tmp/jeroenclaes$  ## edit step here copying SO post code
edd@brad:/tmp/jeroenclaes$

我们创建胶水

请注意,compAttr.r 只是我小孩子的一个微不足道的 shell 包装器 包裹。这里没有魔法。请注意,我只 运行 它一次。 (使用现有包时,有时会出现 运行 两次问题。)

edd@brad:/tmp/jeroenclaes$ compAttr.r
edd@brad:/tmp/jeroenclaes$ 

接下来我们构建包

edd@brad:/tmp/jeroenclaes$ cd ..
edd@brad:/tmp$ R CMD build jeroenclaes
* checking for file ‘jeroenclaes/DESCRIPTION’ ... OK
* preparing ‘jeroenclaes’:
* checking DESCRIPTION meta-information ... OK
* cleaning src
* installing the package to process help pages
* saving partial Rd database
* cleaning src
* checking for LF line-endings in source and make files
* checking for empty or unneeded directories
* building ‘jeroenclaes_1.0.tar.gz’
edd@brad:/tmp$ 

并检查它

edd@brad:/tmp$ R CMD check jeroenclaes_1.0.tar.gz
* using log directory ‘/tmp/jeroenclaes.Rcheck’
* using R version 3.4.1 (2017-06-30)
* using platform: x86_64-pc-linux-gnu (64-bit)
* using session charset: UTF-8
* checking for file ‘jeroenclaes/DESCRIPTION’ ... OK
* checking extension type ... Package
* this is package ‘jeroenclaes’ version ‘1.0’
* checking package namespace information ... OK
* checking package dependencies ... OK
* checking if this is a source package ... OK
* checking if there is a namespace ... OK
* checking for executable files ... OK
* checking for hidden files and directories ... OK
* checking for portable file names ... OK
* checking for sufficient/correct file permissions ... OK
* checking whether package ‘jeroenclaes’ can be installed ... [12s/11s] OK
* checking installed package size ... OK
* checking package directory ... OK
* checking DESCRIPTION meta-information ... OK
* checking top-level files ... OK
* checking for left-over files ... OK
* checking index information ... OK
* checking package subdirectories ... OK
* checking R files for non-ASCII characters ... OK
* checking R files for syntax errors ... OK
* checking whether the package can be loaded ... OK
* checking whether the package can be loaded with stated dependencies ... OK
* checking whether the package can be unloaded cleanly ... OK
* checking whether the namespace can be loaded with stated dependencies ... OK
* checking whether the namespace can be unloaded cleanly ... OK
* checking loading without being on the library search path ... OK
* checking dependencies in R code ... OK
* checking S3 generic/method consistency ... OK
* checking replacement functions ... OK
* checking foreign function calls ... OK
* checking R code for possible problems ... OK
* checking Rd files ... OK
* checking Rd metadata ... OK
* checking Rd cross-references ... OK
* checking for missing documentation entries ... WARNING
Undocumented code objects:
  ‘mcc’
All user-level objects in a package should have documentation entries.
See chapter ‘Writing R documentation files’ in the ‘Writing R
Extensions’ manual.
* checking for code/documentation mismatches ... OK
* checking Rd \usage sections ... OK
* checking Rd contents ... OK
* checking for unstated dependencies in examples ... OK
* checking line endings in C/C++/Fortran sources/headers ... OK
* checking compiled code ... OK
* checking examples ... OK
* checking PDF version of manual ... OK
* DONE

Status: 1 WARNING
See
  ‘/tmp/jeroenclaes.Rcheck/00check.log’
for details.


edd@brad:/tmp$

我觉得还不错。

完整性

edd@brad:/tmp$ R CMD INSTALL jeroenclaes_1.0.tar.gz
* installing to library ‘/usr/local/lib/R/site-library’
* installing *source* package ‘jeroenclaes’ ...
** libs
ccache g++ -I/usr/share/R/include -DNDEBUG  -I"/usr/local/lib/R/site-library/Rcpp/include"    -fpic  -g -O3 -Wall -pipe -Wno-unused -pedantic -Werror -march=native -c RcppExports.cpp -o RcppExports.o
ccache g++ -I/usr/share/R/include -DNDEBUG  -I"/usr/local/lib/R/site-library/Rcpp/include"    -fpic  -g -O3 -Wall -pipe -Wno-unused -pedantic -Werror -march=native -c mcc.cpp -o mcc.o
ccache g++ -I/usr/share/R/include -DNDEBUG  -I"/usr/local/lib/R/site-library/Rcpp/include"    -fpic  -g -O3 -Wall -pipe -Wno-unused -pedantic -Werror -march=native -c rcpp_hello_world.cpp -o rcpp_hello_world.o
ccache g++ -Wl,-S -shared -L/usr/lib/R/lib -Wl,-Bsymbolic-functions -Wl,-z,relro -o jeroenclaes.so RcppExports.o mcc.o rcpp_hello_world.o -L/usr/lib/R/lib -lR
installing to /usr/local/lib/R/site-library/jeroenclaes/libs
** R
** preparing package for lazy loading
** help
*** installing help indices
** building package indices
** testing if installed package can be loaded
* DONE (jeroenclaes)
edd@brad:/tmp$ R

R version 3.4.1 (2017-06-30) -- "Single Candle"
Copyright (C) 2017 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

  Natural language support but running in an English locale

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

> library(jeroenclaes)
> mcc("The quick brown fox")
[1] "0"
>

可能是 macOS 特有的。我不知道 - 也许在 r-sig-mac.

上问

我今天遇到了同样的错误。我的解决方案是直接按名称调用函数替换 .Call 方法,它神奇地起作用了。

就把我的经验分享给大家吧