0
votes

I have this code with function which takes two-dimensional array by reference and its bounds by template as an arguments:

#include <stdio.h>
void Foo(); // I need it here

int main()
{
    char Space[10][10];
    Foo(Space);

    return 0;
}

template <size_t rows, size_t cols>
void Foo(char (&array)[rows][cols])
{
    size_t j;
    size_t i;
    for (j = 0; j < rows; j++)
    {
        for (i = 0; i < cols; i++)
        {          
            array[i][j] = '.';
        }
    }
}

I need to declare this function before main code block, and then define after it. How to do this properly?

2

2 Answers

0
votes

Just remove the function body:

template <size_t rows, size_t cols>
void Foo(char (&array)[rows][cols]);
0
votes

Just put that declaration up where you want it:

#include <stdio.h>
template <size_t rows, size_t cols>
void Foo(char (&array)[rows][cols]);

int main()
{
    char Space[10][10];
    Foo(Space);

    return 0;
}

template <size_t rows, size_t cols>
void Foo(char (&array)[rows][cols])
{
    size_t j;
    size_t i;
    for (j = 0; j < rows; j++)
    {
        for (i = 0; i < cols; i++)
        {          
            array[i][j] = '.';
        }
    }
}

Godbolt example