固定大小数组作为函数参数:无匹配函数,可以调用到' begin'

发布于 2025-02-10 04:53:11 字数 591 浏览 3 评论 0原文

我将固定大小数组传递给一个函数(将大小定义为函数定义中的常数)。但是,我仍然有错误

无匹配功能呼叫“开始”

# define arr_size 2
void test(int arr0[2]){
    int arr1[]={1,2,3};
    int arr2[arr_size];
    
    begin(arr0); // does not work -- how can I make this work?
    begin(arr1); // works
    begin(arr2); // works
}

相关的讨论在这里< /a>但是,在这种情况下,阵列的大小显然并不恒定。我想避免出于效率原因避免使用向量(如在那里所建议的)。

有人知道问题是什么吗?

I am passing a fixed size array to a function (the size is defined to a constant in the function's definition). However, I still get the error

No matching function for call to 'begin'

# define arr_size 2
void test(int arr0[2]){
    int arr1[]={1,2,3};
    int arr2[arr_size];
    
    begin(arr0); // does not work -- how can I make this work?
    begin(arr1); // works
    begin(arr2); // works
}

There is a related discussion here, however, the array's size was clearly not constant in that case. I want to avoid using vectors (as suggested there) for efficiency reasons.

Does anyone know what the issues is?

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

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

发布评论

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

评论(1

日暮斜阳 2025-02-17 04:53:11

此函数声明

void test(int arr0[2]){

等效于此,

void test(int *arr0){

因为编译器会调整具有数组类型的参数,以指示数组元素类型。

这两个声明都声明了相同的一个函数。

您甚至可以写作,例如

void test(int arr0[2]);

void test(int *arr0){
    //,,,
}

,您正在尝试调用函数begin

begin(arr0);

您可以将参数声明为对数组类型的引用

void test(int ( &arr0 )[2]){

,以抑制从数组到指针的隐式转换。

This function declaration

void test(int arr0[2]){

is equivalent to

void test(int *arr0){

because the compiler adjusts parameters having array types to pointers to array element types.

That is the both declarations declare the same one function.

You may even write for example

void test(int arr0[2]);

void test(int *arr0){
    //,,,
}

So you are trying to call the function begin for a pointer

begin(arr0);

You could declare the parameter as a reference to the array type

void test(int ( &arr0 )[2]){

to suppress the implicit conversion from an array to a pointer.

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