【问题标题】:C++ help with error : cannot convert parameter 1 from 'float' to 'float [][2]C++ 错误帮助:无法将参数 1 从 'float' 转换为 'float [][2]
【发布时间】:2011-01-29 19:31:59
【问题描述】:

我正在做一个程序,这并不重要。显然,我无法将参数发送到浮点函数。 代码看起来像这样

float myfunction(float array[1][2])
{
// ...
return 0;
}

int main()
{
float array[1][2];
int foo = 0;
// assigning values to the array

foo = myfunction(array[1][2]);
return 0;
}

当我尝试编译时,我收到错误“无法将参数 1 从 'float' 转换为 'float [][2]” 怎么了?我该如何解决?

【问题讨论】:

  • 好的,我明白我的错误了。感谢您的帮助!

标签: c++ arrays function


【解决方案1】:

只传递数组,不带索引:

foo = myfunction(array);

【讨论】:

    【解决方案2】:

    几件事。

    首先,原型为float myfunction(float array[1][2]) 的函数令人困惑(),因为它实际上的意思是:float myfunction(float array[][2])float myfunction(float (*array)[2])。该函数接受一个指向(一个或多个)两个浮点数数组的指针。

    第二,你得到的错误是因为函数接受一个指向数组的指针,而你试图传递它 single float - 二维的元素 [1][2]数组float array[1][2]。也许您打算将整个数组传递给函数?

    【讨论】:

    • 哦。我现在感觉有点傻,因为我自己没有弄清楚,但谢谢你的回答!
    【解决方案3】:

    您已经定义了变量:float array[1][2]; 然后,您以这种方式调用该函数:foo = myfunction(array); 作为参数,只有你必须设置变量名。你不应该这样做:foo = myfunction(array[1][2]);

    当您创建一个函数时,请按以下方式进行:输入 myfunction(float Array[][w]) ,输入函数类型(void、float...)和“w”一个常量整数。

    【讨论】:

      【解决方案4】:

      您传递的是某个单元格而不是数组

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多