函数调用不能与C++中的候选模板定义(通过引用接收2D数组的函数)匹配。

发布于 2025-02-03 23:10:21 字数 2411 浏览 3 评论 0原文

这里的新手尝试一种不同的方法来传递C ++中的数组。
对于C ++, geeksforgeeks (标题为标题, 模板方法(引用数组))通过创建模板通过C ++中的参考来传递数组。我之所以尝试它,是因为这似乎是不使用指针而仍在每个不同函数调用中的不同尺寸的数组的一种方法。

在Geeksforgeeks的以下代码中注意,为数组的大小指定了模板参数。

// 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);
}

我试图通过按下AA模板来扩展2D数组的逻辑:

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() {
        
//     }
};

运行此代码后,我会发现一个错误,

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.

我简直无法想到解决此错误的方法。我知道,即使它们是相同的,它也无法匹配返回类型。

整个逻辑是用于解决

这是问题描述的一部分:

您有一个n个正整数的数字。

您可以多次在数组的任何元素上执行两种类型的操作:

如果元素是偶数,则将其除以2。 例如,如果数组为[1,2,3,4],则可以在最后一个元素上执行此操作,并且数组将为[1,2,3,2]。

如果元素是奇数,则将其乘以2。 例如,如果数组为[1,2,3,4],则可以在第一个元素上执行此操作,并且数组将为[2,2,3,4]。

数组的偏差是数组中任意两个元素之间的最大差异。

返回执行一些操作后数量的最小偏差。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

清醇 2025-02-10 23:10:21

问题float r [2] [nums.size()];不是标准的C ++,因为数组的大小必须为编译时间常数

但是,由于nums.size()不是常数表达式,因此不能用来指定数组的大小,而且不能用作模板非类型参数作为模板非型参数必须是一个编译时间常数。

您可以通过更改nums.size()float r [2] [2] [2]中使用常数表达式来验证这一点。 nums.size()];,您会发现上述错误已经消失。

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

demo,没有编译时间错误

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 so it cannot be used to specify the size of an array and moreover 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文