我试图在Rcpp函数中打开一个文件,所以我需要一个字符*或std::string形式的文件名。
到目前为止,我已经尝试了以下方法:
#include <Rcpp.h>
#include <boost/algorithm/string.hpp>
#include <fstream>
#include <string>
RcppExport SEXP readData(SEXP f1) {
Rcpp::CharacterVector ff(f1);
std::string fname = Rcpp::as(ff);
std::ifstream fi;
fi.open(fname.c_str(),std::ios::in);
std::string line;
fi >> line;
Rcpp::CharacterVector rline = Rcpp::wrap(line);
return rline;
}
但是很明显,as
不适用于Rcpp::CharacterVector
,因为我得到了一个编译时错误。
foo.cpp: In function 'SEXPREC* readData(SEXPREC*)':
foo.cpp:8: error: no matching function for call to 'as(Rcpp::CharacterVector&)'
make: *** [foo.o] Error 1
有没有一种简单的方法可以从参数中获取字符串,或者以某种方式从Rcpp函数参数中打开文件?
发布于 2011-12-08 04:49:11
Rcpp::as()
需要SEXP
作为输入,而不是Rcpp::CharacterVector
。尝试将f1
参数直接传递给Rcpp::as()
,例如:
std::string fname = Rcpp::as(f1);
或者:
std::string fname = Rcpp::as<std::string>(f1);
发布于 2012-11-10 02:43:47
真正的问题是,Rcpp::as
要求您手动指定要转换为的类型,例如Rcpp::as<std::string>
。
所有as
重载的输入总是一个SEXP
,因此编译器不知道使用哪一个,也不能自动做出决定。这就是为什么你需要帮助它。对于wrap
来说,事情是不同的,它可以使用输入类型来决定将使用哪个重载。
https://stackoverflow.com/questions/8421250
复制相似问题