【问题标题】:Rcpp: how to set the character parameter in a function?Rcpp:如何在函数中设置字符参数?
【发布时间】:2013-05-14 05:41:57
【问题描述】:

我是Rcpp的初学者。 我能问一个非常基本的问题吗?以下是简单代码:

#include <Rcpp.h>
using namespace Rcpp;

// [[Rcpp::export]]
int test(char d) {
char c;
c=d;
return 0;
}

但是当我尝试编译它时,我总是收到如下错误:

/usr/local/genomics/programs/R-3.0.0/library/Rcpp/include/Rcpp/as.h: In function ‘T   Rcpp::internal::as_string(SEXPREC*, Rcpp::traits::false_type) [with T = char]’:
/usr/local/genomics/programs/R-3.0.0/library/Rcpp/include/Rcpp/as.h:66:   instantiated from ‘T Rcpp::internal::as(SEXPREC*, Rcpp::traits::r_type_string_tag) [with T = char]’
/usr/local/genomics/programs/R-3.0.0/library/Rcpp/include/Rcpp/as.h:126:   instantiated from ‘T Rcpp::as(SEXPREC*) [with T = char]’
test1.cpp:18:   instantiated from here
/usr/local/genomics/programs/R-3.0.0/library/Rcpp/include/Rcpp/as.h:62: error: invalid conversion from ‘const char*’ to ‘char’
make: *** [test1.o] Error 1
g++ -I/usr/local/genomics/programs/R-3.0.0/include -DNDEBUG  -I/usr/local/include  -I"/usr/local/genomics/programs/R-3.0.0/library/Rcpp/include"    -fpic  -g -O2  -c test1.cpp -o test1.o

sourceCpp("test1.cpp") 中的错误: 构建共享库时出现错误 1。

你能告诉我会发生什么吗?非常感谢!

【问题讨论】:

    标签: r rcpp


    【解决方案1】:

    从某种意义上说,这是一个错误,因为我们可以支持单个 char 对象。

    另一方面,这并不重要,因为你可以用一个字符做的有用的东西太少了。如果您改为使用int,它就会起作用

    #include <Rcpp.h>
    using namespace Rcpp;
    
    // [[Rcpp::export]]
    int mytest(int d) {
      int c;
      c=d;
      return 0;
    }
    

    或者,更好的是,使用string 类型:

    #include <Rcpp.h>
    using namespace Rcpp;
    
    // [[Rcpp::export]]
    int mytest(std::string d) {
      std::string c;
      c=d;
      return 0;
    }
    

    当您使用 Rcpp 自己的类型 CharacterVector 时,它当然可以工作。

    #include <Rcpp.h>
    using namespace Rcpp;
    
    // [[Rcpp::export]]
    int mytest(CharacterVector d) {
      CharacterVector c;
      c=d;
      return 0;
    }
    

    单个 char 变量在 C 和 C++ 中有点奇怪(您需要它们的数组或指针)来表达“单词”。所以解决这个问题并没有真正的用处,因为 R 无论如何都只有向量类型。

    【讨论】:

    • 然而这是一个“错误”,所以我们登录并查看是否可以修复。正如我所指出的,这并不是什么非常重要的事情。随意选择“勾选标记”来“接受”答案。
    猜你喜欢
    • 1970-01-01
    • 2019-07-08
    • 2015-12-11
    • 1970-01-01
    • 2017-10-05
    • 2015-02-11
    • 1970-01-01
    • 2023-04-02
    • 1970-01-01
    相关资源
    最近更新 更多