Yes I do know how to actually create a reference to a c style function.
But when I try to pass an array by reference it gives me the “no matching function” error.
I first take in the array by value, so when I pass it I have to create a reference.
I can use a std::vector to store it (and I probably will), however now that I’ve encountered this problem I’m curious to the solution.
Here’s the simplified example:
void foo((&array)[120]){
// code here
}
// taken by value
void bar(array[120]){
foo((&array));
// doesn't work
}
// This version does not work either
void bar(array[120]){
Type *arrRef = {(&array)[120]};
foo(arrRef);
}
alex conklin is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
3
You need to use templates, and make the size a template argument:
template<size_t N>
void foo(int (&array)[N])
{
// ...
}
void bar()
{
int arr[32];
foo(arr);
}
Note that the &
operator means different things in different contexts. When declaring a variable it means reference. When using it in a unary expression (like e.g. &array
) then it’s the pointer-to operator, and if using it in a binary expression it’s the bitwise AND operator.
1