在 Rcpp 中从 'SEXP' 到 long int 的无效转换
invalid conversion from 'SEXP' to long int in Rcpp
我正在尝试 运行 我的 cpp 程序与 R 中 dlib 的矩阵。几个小时以来,我一直在尝试 运行 代码,但我无法解决一个错误。你能帮帮我吗?
有我的代码片段:
#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.
这是怎么回事?我不明白:(
编辑:
仅当我在使用 column_vector
的函数前添加 // [[Rcpp::export]]
时才会出现此错误
编辑有帮助。我之前的评论被关闭了。所以基本上,你 typedef this
typedef matrix<double,0,1> column_vector;
然后使用 column_vector
并期望 Rcpp 神奇地转换其内容。
简而言之,不是那样的。如果您有 新的 类型而 Rcpp 还不知道 那么负担是 您 提供新转换器。
我们写了一个关于这个的 vignette 'Extending Rcpp' 可能会有帮助。
我正在尝试 运行 我的 cpp 程序与 R 中 dlib 的矩阵。几个小时以来,我一直在尝试 运行 代码,但我无法解决一个错误。你能帮帮我吗?
有我的代码片段:
#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.
这是怎么回事?我不明白:(
编辑:
仅当我在使用 column_vector
// [[Rcpp::export]]
时才会出现此错误
编辑有帮助。我之前的评论被关闭了。所以基本上,你 typedef this
typedef matrix<double,0,1> column_vector;
然后使用 column_vector
并期望 Rcpp 神奇地转换其内容。
简而言之,不是那样的。如果您有 新的 类型而 Rcpp 还不知道 那么负担是 您 提供新转换器。
我们写了一个关于这个的 vignette 'Extending Rcpp' 可能会有帮助。