c++arraysfunction-templates

Function argument list doesn't match function template (function template is made for passing stack allocated array)


I made function template to pass stack-allocated array as parameter, which is shown below:

struct Pos {
    int row;
    int col;
    Pos(int r, int c) {
        row = r; col = c;
    }
    Pos() {
        row = 0; col = 0;
    }
};

template <int R, int C>
void traverse(int (&matrix)[R][C], bool (&visited)[R][C], Pos& target) {
          // code inside function
};

I tried to call function with arguments inside 'int main()' like below:

int matrix[rowSize][colSize];
bool visited[rowSize][colSize];

Pos target;
target.row = 0;
target.col = 0;

traverse(matrix, visited, target);

When I try to run the code with g++, it gives me error: no instance of function template "traverse" matches the argument list.

Any suggestions or corrections?

Thanks in advance.

I tried to pass traverse(&matrix, &visited, target) traverse(*matrix, *visited, target)

I knew it won't work but I was just desperate.


Solution

  • The issue seems to be maybe from rowSize and colSize declaration , which arent deduced correctly at compiling time , if you have rowSize and colSize as integers it would cause an error as they should be constants ,so i guess you need to add in your main : constexpr int rowSize and constexpr int rowSize (i hope this helps)