Search code examples
rcpp

Rcpp function call from function in the same cpp file


Hi i have this rcpp file :

#include <Rcpp.h>

using namespace Rcpp;

// [[Rcpp::export]]

int ricercamin (NumericVector p, NumericVector u, int n1){

   int trovato=0;
int m;
int i=0;
while(trovato==0 & i<n1){
    if(u[i]==1){
        trovato=1;
        m=i;
    }
    else{
        i++;
    }
}
for(i=0;i<n1;i++){
    if(p[i]<p[m]){
        m=i;
    }
}
return m;
}

}

// [[Rcpp::export]]

NumericMatrix trai (NumericMatrix weigth, CharacterVector src, CharacterVector rn){

int n=weigth.nrow();
NumericVector potenziale[n];
NumericVector predecessore [n];
NumericVector insieme [n];
int i,j,k;
 for(i=0;i<n;i++){
    j=ricercamin(potenziale,insieme,n);


.....
}

The error is : use of undeclared identifier 'ricercamin'

How can i solve this ?

it seems like it does not found the function but the function is in the same cpp file and the declaration is before the call of the fun.


Solution

  • Your example code does not compile since the braces are not balanced and a literal .... within the source code is probably not want you want. Anyway, I have reduced your code to what I believe to be the heart of the issue:

    #include <Rcpp.h>
    
    using namespace Rcpp;
    
    // [[Rcpp::export]]
    int ricercamin (NumericVector p, NumericVector u, int n1){
        return 0;
    }
    
    
    // [[Rcpp::export]]
    NumericMatrix trai (NumericMatrix weigth, CharacterVector src, CharacterVector rn){
    
        int n=weigth.nrow();
        NumericVector potenziale[n];
        NumericVector insieme [n];
        int i,j;
        for(i=0;i<n;i++){
            j=ricercamin(potenziale,insieme,n);
        }
        return weigth;
    }
    

    Compiling this I do not get the quoted message but instead:

    58911878.cpp: In function ‘Rcpp::NumericMatrix trai(Rcpp::NumericMatrix, Rcpp::CharacterVector, Rcpp::CharacterVector)’:
    58911878.cpp:19:22: error: could not convert ‘(Rcpp::NumericVector*)(& potenziale)’ from ‘Rcpp::NumericVector*’ {aka ‘Rcpp::Vector<14, Rcpp::PreserveStorage>*’} to ‘Rcpp::NumericVector’ {aka ‘Rcpp::Vector<14, Rcpp::PreserveStorage>’}
       19 |         j=ricercamin(potenziale,insieme,n);
          |                      ^~~~~~~~~~
          |                      |
          |                      Rcpp::NumericVector* {aka Rcpp::Vector<14, Rcpp::PreserveStorage>*}
    make: *** [/usr/lib/R/etc/Makeconf:176: 58911878.o] Error 1
    

    Now this error message tells me something useful: The function ricercamin expects a NumericVector, but I am giving it a pointer to a NumericVector. The correct fix depends on what the variable potenziale is supposed to contain. Since you mention "a NumericVector of size n" in the comments, you should use (n) instead of [n], since the latter declares an array of NumericVectors:

    #include <Rcpp.h>
    
    using namespace Rcpp;
    
    // [[Rcpp::export]]
    int ricercamin (NumericVector p, NumericVector u, int n1){
        return 0;
    }
    
    
    // [[Rcpp::export]]
    NumericMatrix trai (NumericMatrix weigth, CharacterVector src, CharacterVector rn){
    
        int n=weigth.nrow();
        NumericVector potenziale(n);
        NumericVector insieme(n);
        int i,j;
        for(i=0;i<n;i++){
            j=ricercamin(potenziale,insieme,n);
        }
        return weigth;
    }
    

    This compiles without errors or warnings for me.