代码一直说:“[错误]无法将参数'1'的'float*'转换为'float'到'void test(flo

Code Keeps saying: "[Error] cannot convert 'float*' to 'float' for argument '1' to 'void test(float, int)'"

本文关键字:float 转换 flo test void 一直 错误 代码 参数      更新时间:2023-10-16
#include <iostream>
using namespace std;
void test(float, int);
int main()
{ 
    const int size=11;
    float a[size];
    test(a, size);
    return 0;
}
void test(float a[], int size)
{
    [....]
}

它指向测试(a,大小);但我无法弄清楚出了什么问题(我也在学习编码,刚刚了解数组/困惑)

您的函数原型void test(float, int);与您的函数void test(float a[], int size)不匹配。将顶部的原型更改为void test(float a[], int size);(为了保持一致性,我喜欢将输入变量名称保留在原型中,但这不是必需的)。

你可能想写:

void test(float*, int);
// ...
void test(float* a, int size)
{
    [....]
}

当使用数组参数调用 test 时,数组将衰减为指向其第一个元素的指针 - 并且其大小将丢失。

测试前向减速的参数类型错误。

试试这个。

#include <iostream>
using namespace std;
void test(float *, int);
int main()
{ 
    const int size=11;
    float a[size];
    test(a, size);
    return 0;
}
void test(float a[], int size)
{
}