-1

I am having trouble inputting a 3D array as an argument to a function in c++. I took out a lot of the code, leaving the minimum amount to get what is going on. The error I am getting is:

"no matching function for call to 'spatial_correlation'
                spatial_correlation(Lattice, L, S, D, distances, types, space_corr);
                ^~~~~~~~~~~~~~~~~~~"

"note: candidate function not viable: no known conversion from 'float [4][4][D]' to 'float (*)[4][D]' for 7th argument
void spatial_correlation(char Lattice[S], const int L, const int S, const int D, vector<float> distances," 
#include <vector>

const int L = 10;
const int S = L*L;

void spatial_correlation(char Lattice[S], const int L, const int S, const int D,
 vector<float> distances, char types[4], float space_corr[4][4][D])
{
     //Finds the spatial correlation. Code not relevant
}

int main()
{
    char types[4] = {'A','B','C','D'};
    vector<float> distances;
    get_distances(Lattice, L, S, distances);
    const int D = distances.size();
    float space_corr[4][4][D];

    spatial_correlation(Lattice, L, S, D, distances, types, space_corr);

    return 0;
}
user4581301
  • 33,082
  • 7
  • 33
  • 54
Jason M Gray
  • 161
  • 1
  • 5
  • 3
    C++ doesn't normally allow you to specify array dimensions as function parameters. – user4581301 Jun 30 '21 at 20:42
  • 3
    there are many more errors from this code. After adding `using namespace std;` I still get those: https://godbolt.org/z/3q4GKx6Ko. What is `Lattice` (the one in `main`)? what is `get_distances`? The error you claim is not among them. What compiler are you using? – 463035818_is_not_an_ai Jun 30 '21 at 20:44
  • Please make sure the code you post here is the code that produces the error you want to get fixed. Read about [mcve] – 463035818_is_not_an_ai Jun 30 '21 at 20:45
  • 2
    In `const int D = distances.size();`, `D` is only constant in the "Can't be changed after initialization" sense. It's not a compile-time constant, and this isn't legal to use as an array dimension in Standard C++. – user4581301 Jun 30 '21 at 20:46
  • 1
    You are trying to use variable-length-arrays. C++ doesn't support them. – Ben Voigt Jun 30 '21 at 20:59
  • 1
    If you're going to try to convert C to C++, you should spend a little time becoming more familiar with both. – 3Dave Jun 30 '21 at 21:18
  • The crux of the issue here is with passing a multidimensional array "C style" for which you need to use an unusual syntax. See here for a reasonable explanation for a 2D case -> https://stackoverflow.com/a/17569578/1607937 See here for a 3D example -> https://stackoverflow.com/questions/13326021/how-to-pass-a-3d-array-as-a-parameter-to-function-c-also-do-global-variables and https://stackoverflow.com/questions/29605133/passing-3d-array-as-parameter-to-a-function-in-c – Den-Jason Jun 30 '21 at 22:13
  • @Den-Jason that's all true, but the real kicker is trying to pass a variable length array. All but the first dimension needs to be known at compile time, and in this case the first dimension is known but the last isn't. This array trick would work in C99, though. – user4581301 Jun 30 '21 at 22:50

1 Answers1

1

Here is a starting point that builds, using Modern C++ idioms, and on which you can expand.

#include <vector>
#include <array>
  
void spatial_correlation(
    const std::vector<char>& Lattice,
    int L,   // const no needed, it's a copy here
    const std::vector<float>& distances,
    const std::array<char,4>& types,
    const std::array<std::array<std::vector<float>,4>,4>& space_corr
)
{
    auto D = distances.size();
    auto S = L*L;
     //Finds the spatial correlation. Code not relevant
}

int main()
{
    std::array<char,4> types = {'A','B','C','D'};
    std::vector<float> distances;
//    get_distances(Lattice, L, distances);    ? ? ?
    std::vector<char> Lattice;

    std::array<std::array<std::vector<float>,4>,4> space_corr;

    spatial_correlation(Lattice, L, distances, types, space_corr);
}
kebs
  • 6,387
  • 4
  • 41
  • 70