2013-05-14 70 views
1

我是Rcpp的初學者。 我可以問一個非常基本的問題嗎?下面是簡單的代碼: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發生建設共享庫。

你能告訴我會發生什麼嗎?非常感謝你!

回答

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只有矢量類型。

+0

非常感謝! – user2380245 2013-05-15 00:52:42

+0

然而,它是一個'錯誤',所以我們登錄並會看看我們是否可以修復。不像我指出的那樣非常重要。通過選擇'tickmark'可以自由地'接受'答案。 – 2013-05-15 00:55:16