2

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
3
  • 3
    arr is a 1D array. Commented Jul 2, 2021 at 19:55
  • 1
    I recommend you look into RcppArmadillo for proper (C++) vectors and matrices made for linear algebra. There may even be a convolution implementation somewhere though I cannot immediately think of one. Commented Jul 2, 2021 at 19:57
  • @DirkEddelbuettel thank you! i'll look into RcppArmadillo right away. Commented Jul 2, 2021 at 19:59

1 Answer 1

2

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].

Sign up to request clarification or add additional context in comments.

3 Comments

ah! so i should put float arr[][] in the arguments?
No you need proper types for 2d behaviour. See my earlier comment.
@JadaLovelace more or less, the second dimension of the array must be specified in the argument, e.g.: arr[][10].

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.