Home > database >  Function call cannot be matched to a candidate template definition (of a function to receive 2D arra
Function call cannot be matched to a candidate template definition (of a function to receive 2D arra

Time:06-03

Novice here trying a different method to pass array-by-reference in C .
For C , geeksforgeeks (under title Template Approach (Reference to Array)) shows a way to pass array by reference in C by creating a template. I am trying it because it seems a way to not use pointers and still pass arrays of different sizes on every different function call.

Notice in the following code from geeksforgeeks, a template parameter is specified for the size of the array.

// CPP Program to demonstrate template approach
#include <iostream>
using namespace std;

template <size_t N> void print(int (&a)[N])
{
    for (int e : a) {
        cout << e << endl;
    }
}

// Driver Code
int main()
{
    int a[]{ 1, 2, 3, 4, 5 };
    print(a);
}

I have tried to extend the logic for 2D arrays by making a a template as followed:

template <size_t r, size_t c>
float approach_mean(vector<int>& n, float (&a)[r][c], float m, float d) {
    return 0;
}

class Solution {
public:
    int minimumDeviation(vector<int>& nums) {
        float m = accumulate(nums.begin(), nums.end(), 0) / nums.size();
        float dev = 0, devp = 0;
        long double s = 0;
        float r[2][nums.size()];
        for (int i0 = 0; i0 < nums.size();   i0) {
            r[0][i0] = nums.at(i0);
            r[1][i0] = m - nums.at(i0);
            dev = dev   abs(m - nums.at(i0));
        }
        dev = dev / nums.size();
        while (devp < dev) {
            devp = dev;
            dev = approach_mean(nums, r, m, dev);
            break;
        }
        return devp;
    }
    
//     float approach_mean() {
        
//     }
};

Upon running this code, I get an error

Line 21: Char 19: error: no matching function for call to 'approach_mean'
            dev = approach_mean(nums, r, m, dev);
                  ^~~~~~~~~~~~~~~
Line 2: Char 7: note: candidate template ignored: could not match 'float' against 'float'
float approach_mean(vector<int>& n, float (&a)[r][c], float m, float d) {
      ^
1 error generated.

I simply can't think of ways to solve this error. I understand that it is unable to match the return type for some reason even though they are the same.

The entire logic is a WIP for a solution to Problem 1675 on Leetcode which is about reducing the deviation in an array.

Here is a part of the description of the problem:

You are given an array nums of n positive integers.

You can perform two types of operations on any element of the array any number of times:

If the element is even, divide it by 2. For example, if the array is [1,2,3,4], then you can do this operation on the last element, and the array will be [1,2,3,2].

If the element is odd, multiply it by 2. For example, if the array is [1,2,3,4], then you can do this operation on the first element, and the array will be [2,2,3,4].

The deviation of the array is the maximum difference between any two elements in the array.

Return the minimum deviation the array can have after performing some number of operations.

CodePudding user response:

The problem is that float r[2][nums.size()]; is not standard C as the size of an array must be a compile time constant.

But as nums.size() is not a constant expression and hence it cannot be used to specify the size of an array and also it cannot be used as a template nontype argument as a template nontype argument must be a compile time constant.

You can verify this by changing nums.size() with some constant expression in float r[2][nums.size()]; and you will find out that the mentioned error is gone.

//-----------------v------>nums.size() replaced with 5
        float r[2][5];

Demo with no compile time error

  • Related