Search code examples
c++arraysrcpp

Why am i getting invalid types float[int] for array subscript?


So i've looked at other questions on stackoverflow that seemed to describe the same problem, but the problem in each of these cases seems to be a wrong reference, e.g. the object was not an array. I think i've referenced my array correctly, but today is my first day doing C++. Could anyone tell me what i'm doing wrong here?

#include <Rcpp.h>
#include <cmath>
using namespace Rcpp;

// [[Rcpp::export]]
float convolute2D1(float arr[], int skew) {
  int m, n, l, j;
  float o;
  if (skew < 0) {
    m = 1;
    n = 2;
    skew = abs(skew);
  } else {
    m = 2;
    n = 1;
  }
  l = *(&arr + 1) - arr;
  l = l / 2 - skew;
  for(int i = 1; i <= l; i++) {
    j = i + skew;
    o = o + abs(arr[m][j] - arr[n][j]);
  }
  return o / l;
}

// END OF SCRIPT

i'm getting the errors:

Line 31 invalid types 'float[int]' for array subscript
Line 31 invalid types 'float[int]' for array subscript
Line 41 expected ',' or '...' before 'SEXP'
Line 45 expected primary-expression before ']' token
line 46 'skewSEXP' was not declared in this scope
Line 47 expected primary-expression before ']' token

Solution

  • You're passing a linear array as argument of the function yet you are using it as if it was a 2D array, that's not possible.

    As it's passed, arr can only be used like arr[i], not like arr[i][j].