Rcpp:'operator=' 矩阵和列表的不明确重载

Rcpp: ambiguous overload for 'operator=' Matrix and List

以下 Rcpp 代码是生成相同编译错误的更大代码的最小可重现示例。似乎我无法将数字矩阵分配给列表,然后再将列表分配给另一个矩阵。

#include <Rcpp.h>
using namespace Rcpp;

//[[Rcpp::export]]
List return_a(NumericMatrix a, NumericMatrix b){
    //the function only returns the input matrix a
    List result(1);
    result(0) = a;
    return(result);
}


//[[Rcpp::export]]
List wrapper_cpp(NumericMatrix a, NumericMatrix b){
    //the function is a dummy wrapper for much more code
    List Step1(1);
    List results(1);    
    Step1 = return_a(a,b);
    a = Step1(0);   
    results(0) = a;
    return(results);
}

上面的代码给出了我缩短的以下编译错误:

error: ambiguous overload for 'operator=' (operand types are 'Rcpp::NumericMatrix {aka Rcpp::Matrix<14>}' and 'Rcpp::Vector<19>::Proxy ...
a = Step1(0);

我的实际功能要复杂得多。我需要在多个循环中操作矩阵,并且在每个步骤中,列表中的每个函数都返回矩阵。然后我需要提取这些列表以进一步操作矩阵。如何才能做到这一点?

除了@Ralf 已经提到的错误之外,您只是尝试了太多。有时我们需要一个中间步骤,因为模板魔术是......挑剔的。以下作品。

代码

#include <Rcpp.h>
using namespace Rcpp;

//[[Rcpp::export]]
List return_a(NumericMatrix a, NumericMatrix b){
  //the function only returns the input matrix a
  List result(1);
  result(0) = a;
  return(result);
}


//[[Rcpp::export]]
List wrapper_cpp(NumericMatrix a, NumericMatrix b){
  //the function is a dummy wrapper for much more code
  List results(1);
  List Step1 = return_a(a,b);
  NumericMatrix tmp = Step1(0);
  results(0) = tmp;
  return(results);
}

输出

R> Rcpp::sourceCpp("~/git/Whosebug/54771818/answer.cpp")
R> wrapper_cpp(matrix(1:4,2,2), matrix(4:1,2,2))
[[1]]
     [,1] [,2]
[1,]    1    3
[2,]    2    4

R>