I am trying to call a templated function from my main. I previously used this sort of syntax and it worked as shown here:
#include <iostream>
using namespace std;
// One argument in template
template<int size>
void print(char matrix[][size]) {
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
cout << matrix[i][j];
}
cout << endl;
}
return;
}
// MAIN
int main() {
constexpr int n = 3;
char matrix[n][n] = {'a', 'b', 'c',
'h', 'i', 'd',
'g', 'f', 'e'};
print<n>(matrix); // Calling function like this
return 0;
}
I am trying to apply this same method here in main using a multi-parameter template but it does not work. I get the error: No matching function for call to 'setRowZero'. I don't quite understand why it does not match as I follow the same structure above. It's just one more parameter!
#include <iostream>
using namespace std;
template<int num_rows, int num_cols>
// Set this entire row to 0
void setRowZero(int matrix[num_rows][num_cols], int row, int cols) {
for (int i = 0; i < cols; i++) {
matrix[row][i] = 0;
}
return;
}
int main() {
// Matrix to be changed
int matrix[3][4] = {1, 1, 1, 1,
1, 0, 1, 1,
1, 1, 0, 1};
// Get row and cols
int num_rows = sizeof(matrix)/sizeof(matrix[0]);
int num_cols = sizeof(matrix[0])/sizeof(int);
// Record the position of the 0s
for (int i = 0; i < num_rows; i++) { // rows
for (int j = 0; j < num_cols; j++) { // cols
if (matrix[i][j] == 0) {
rows[i] = 0;
cols[j] = 0;
}
}
}
// Set to 0's
for (int i = 0; i < num_rows; i++) {
if (rows[i] == 0) {
setRowZero<num_rows, num_cols>(matrix, i, num_cols);
}
}
return 0;
}