来自' SEXP'的无效转换在Rcpp中进行long int

时间:2017-09-14 11:59:06

标签: rcpp dlib

我试图用R中的dlib运行我的cpp程序。几个小时我一直试图运行代码,但我无法解决一个错误。你能救我吗?

我的代码有片段:

#include <Rcpp.h>
#include <cmath>
#include <vector>

// [[Rcpp::depends(RcppEigen)]]
#include <RcppEigen.h>

// [[Rcpp::plugins(cpp11)]]
// [[Rcpp::depends(dlib)]]
#include <dlib/optimization/optimization.h>

using namespace Eigen;
using namespace std;
using namespace dlib;

typedef matrix<double,0,1> column_vector;

不幸的是,由于Source Cpp错误,我无法编译代码:

~/R/win-library/3.4/Rcpp/include/Rcpp/internal/Exporter.h
Line 31 invalid conversion from 'SEXP' to long int [-fpermissive]

和来自控制台的日志:

    C:/RBuildTools/3.4/mingw_64/bin/g++  -std=gnu++11 -I"C:/PROGRA~1/R/R-34~1.1/include" -DNDEBUG   -I"C:/Users/user_name/Documents/R/win-library/3.4/Rcpp/include" -I"C:/Users/user_name/Documents/R/win-library/3.4/RcppEigen/include" -I"C:/Users/user_name/Documents/R/win-library/3.4/dlib/include" -I"C:/Users/user_name/Documents/project_name/cpp_to_R"   -I"d:/Compiler/gcc-4.9.3/local330/include"  -std=c++0x   -O2 -Wall  -mtune=core2 -c project_name.cpp -o project_name.o
In file included from C:/Users/user_name/Documents/R/win-library/3.4/Rcpp/include/Rcpp/as.h:25:0,
                 from C:/Users/user_name/Documents/R/win-library/3.4/Rcpp/include/RcppCommon.h:160,
                 from C:/Users/user_name/Documents/R/win-library/3.4/Rcpp/include/Rcpp.h:27,
                 from project_name.cpp:1:
C:/Users/user_name/Documents/R/win-library/3.4/Rcpp/include/Rcpp/internal/Exporter.h: In instantiation of 'Rcpp::traits::Exporter<T>::Exporter(SEXP) [with T = dlib::matrix<double, 0l, 1l>; SEXP = SEXPREC*]':
C:/Users/user_name/Documents/R/win-library/3.4/Rcpp/include/Rcpp/as.h:87:51:   required from 'T Rcpp::internal::as(SEXP, Rcpp::traits::r_type_generic_tag) [with T = dlib::matrix<double, 0l, 1l>; SEXP = SEXPREC*]'
C:/Users/user_name/Documents/R/win-library/3.4/Rcpp/include/Rcpp/as.h:152:82:   required from 'T Rcpp::as(SEXP) [with T = dlib::matrix<double, 0l, 1l>; SEXP = SEXPREC*]'
C:/Users/user_name/Documents/R/win-library/3.4/Rcpp/include/Rcpp/InputParameter.h:72:62:   required from 'Rcpp::ConstReferenceInputParameter<T>::ConstReferenceInputParameter(SEXP) [with T = dlib::matrix<double, 0l, 1l>; SEXP = SEXPREC*]'
project_name.cpp:279:80:   required from here
C:/Users/user_name/Documents/R/win-library/3.4/Rcpp/include/Rcpp/internal/Exporter.h:31:31: error: invalid conversion from 'SEXP' to 'long int' [-fpermissive]
       Exporter( SEXP x ) : t(x){}
                               ^
In file included from C:/Users/user_name/Documents/R/win-library/3.4/dlib/include/dlib/optimization/../matrix.h:6:0,
                 from C:/Users/user_name/Documents/R/win-library/3.4/dlib/include/dlib/optimization/optimization_search_strategies.h:8,
                 from C:/Users/user_name/Documents/R/win-library/3.4/dlib/include/dlib/optimization/optimization.h:9,
                 from project_name.cpp:8:
C:/Users/user_name/Documents/R/win-library/3.4/dlib/include/dlib/optimization/../matrix/matrix.h:1019:18: note: initializing argument 1 of 'dlib::matrix<T, num_rows, num_cols, mem_manager, layout>::matrix(long int) [with T = double; long int num_rows = 0l; long int num_cols = 1l; mem_manager = dlib::memory_manager_stateless_kernel_1<char>; layout = dlib::row_major_layout]'
         explicit matrix (
                  ^
make: *** [project_name.o] Error 1
Warning message:
running command 'make -f "C:/PROGRA~1/R/R-34~1.1/etc/x64/Makeconf" -f "C:/PROGRA~1/R/R-34~1.1/share/make/winshlib.mk" CXX='$(CXX11) $(CXX11STD)' CXXFLAGS='$(CXX11FLAGS)' CXXPICFLAGS='$(CXX11PICFLAGS)' SHLIB_LDFLAGS='$(SHLIB_CXX11LDFLAGS)' SHLIB_LD='$(SHLIB_CXX11LD)' SHLIB="sourceCpp_78.dll" WIN=64 TCLBIN=64 OBJECTS="project_name.o"' had status 2 
Error in Rcpp::sourceCpp("cpp_to_R/project_name.cpp") : 
  Error 1 occurred building shared library.

这里有什么问题?我不明白:(

修改

只有在我使用// [[Rcpp::export]]

的函数之前添加column_vector时才会出现此错误

1 个答案:

答案 0 :(得分:1)

编辑有帮助。我之前的评论已经结束。所以基本上,你输入这个

typedef matrix<double,0,1> column_vector;

然后使用column_vector并期望Rcpp神奇地转换其内容。

简而言之,它不起作用。如果您有新的类型,而Rcpp 尚未了解,那么上的负担就会提供新的转换器。

我们写了vignette 'Extending Rcpp'这个可能会有所帮助。