2
我有一個用RcppArmadillo風格編寫的函數,我想用它來改變調用環境中的變量。我知道這樣做並不可取,但對我來說這很有幫助。具體地說,我試着這樣:函數在RcppArmadillo中引用參考
#include <RcppArmadillo.h>
#include <iostream>
//[[Rcpp::export]]
void myfun(double &x){
arma::mat X = arma::randu<arma::mat>(5,5);
arma::mat Y = X.t()*X;
arma::mat R1 = chol(Y);
x = arma::det(R1);
std::cout << "Inside myfun: x = " << x << std::endl;
}
/*** R
x = 1.0 // initialize x
myfun(x) // update x to a new value calculated internally
x // return the new x; it should be different from 1
*/
我錯過了什麼?爲什麼不工作?