Home > database >  C ; Pass std::array as a Function Parameter
C ; Pass std::array as a Function Parameter

Time:04-26

I know this is a common question, but how do I pass a std::array as a function parameter? I've looked at answers for other questions on here asking the same thing, and the best "solution" I found was to use a template to set size_t to a keyword like SIZE. I tried this, but it still gives me two compile time errors:

Error   C2065   'SIZE': undeclared identifier
Error   C2975   '_Size': invalid template argument for 'std::array', expected compile-time constant expression

Both errors on the line where I have my void function definition.

As far as I know, I did this exactly how the solution was typed out. I know I could instead pass through a std::vector, but I'm stubborn and want to learn this.

Here is my relevant code:

#include <iostream>
#include <array>
using namespace std;

template<size_t SIZE>
void QuickSort(array<unsigned, SIZE>& arrayName);

int main()
{
    // main function.
}

void QuickSort(array<unsigned, SIZE>& arrayName)
{
    // whatever the function does.
}

I'd appreciate any help figuring out what it is that I'm doing wrong.

EDIT: I forgot to take out the second function parameter. I did that, but still the same issues.

CodePudding user response:

Your function definition still needs template parameters since the compiler has no way of knowing what SIZE is

template<size_t SIZE>
// ^^^^^^^^^^^^^^^^^^
void QuickSort(array<unsigned, SIZE>& arrayName)
{
/// whatever the function does.
}
  • Related