Rcpp候选函数具有不同数量的参数(预期为0但具有1)'

时间:2017-08-22 10:27:55

标签: c++ r rcpp

我对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 ++的经验几乎不存在,所以我甚至不理解它在这里说的是什么。谷歌/ Stackoverflow搜索错误消息没有返回任何有用的结果。

感谢您的帮助!

---编辑----

这是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);
}

2 个答案:

答案 0 :(得分:3)

你的代码非常好。 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。

答案 1 :(得分:0)

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

与您分享我的经验。