C++ '无法将参数'1'的'float'转换为'float*'
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28282657/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
'Cannot convert 'float' to 'float*' for argument '1'
提问by Geeh
I'm an ICT student, studying C++, and I find it very interesting. However, while experimenting, I came up to something I can't fix myself nor find on the net. This is the code:
我是一名 ICT 学生,正在学习 C++,我觉得这很有趣。然而,在试验过程中,我发现了一些我自己无法解决的问题,也无法在网上找到。这是代码:
#include <iostream>
using namespace std;
float average(float array[10], float &average){
int i = 0;
for( i = 0; i != 10; i++ ){
cout << "Vett ("<<i<<") = ";
cin >> array[i];
while(cin.fail())
{
cin.clear();
cin.ignore();
system("Color 5C");
cout << "\nPlease insert a number: ";
cin >> array[i];
}
average = average + array[i];
}
average = average / 10;
return array[10];
return average;
}
main(void){
float vett[10], media;
int i;
char loop;
vett[10] = 0;
media = 0;
do{
system("cls");
system("Color 00");
cout<<"****************************************"<<endl;
cout<<"*** INSER THE DATA TO COMPUTE ***"<<endl;
cout<<"****************************************\n"<<endl;
/* for( i = 0; i != 10; i++ ){
cout << "Vett ("<<i<<") = ";
cin >> vett[i];
while(cin.fail())
{
cin.clear();
cin.ignore();
system("Color 5C");
cout << "\nPlease insert a number: ";
cin >> vett[i];
}
media = media + vett[i];
}
media = media / 10;
*/
average(vett[10],media);
for( i = 0; i != 10; i++ ){
cout << vett[i]<<" ";
}
if(media == 0){
cout << "\nATTENTION the average equals to = "<<media<<endl;
}
else{
cout << "\nThe average is"<<media<<endl;
}
printf("\n");
cout << "\nDo You want to continue? Y/N";
cin >> loop;
}
while(loop == 'Y' || loop == 'y');
system("pause");
}
For some reason I couldn't set in the 'average' function the array as a pointer (&array), perhaps because the array is already a pointer. Nonetheless, removing it gives me the following error:
出于某种原因,我无法在“平均”函数中将数组设置为指针(&array),可能是因为该数组已经是一个指针。尽管如此,删除它会给我以下错误:
"Cannot convert 'float' to 'float*' for argument '1' to 'float average(float*,float&)'
If I call the function this way
如果我这样调用函数
average(&vett[10],media);
it works, but returns weird values in the array. As you can see, I commented the same thing I put in the function, which works perfectly, unless..I put it in a function. I assume I've done something wrong with the function call, can anybody help me understand?
它有效,但在数组中返回奇怪的值。正如你所看到的,我评论了我放在函数中的同样的东西,它完美地工作,除非......我把它放在一个函数中。我想我在函数调用上做错了什么,有人能帮我理解吗?
回答by Macmade
First of all, note that main(void){
is not a valid signature for main
.
It should be:
首先,请注意这main(void){
不是 的有效签名main
。
它应该是:
int main(void){
Then:
然后:
float vett[10];
vett[10] = 0;
This is not valid. Array indices start at 0, so index 10 is out of bounds, as it would require an array with size 11.
这是无效的。数组索引从 0 开始,因此索引 10 越界,因为它需要一个大小为 11 的数组。
Also, as your average
function takes as first argument a float array, you'll need to pass it this way:
此外,由于您的average
函数将浮点数组作为第一个参数,因此您需要以这种方式传递它:
average(vett,media);
Using:
使用:
average(&vett[10],media);
Will pass a pointer to the data located right after the array, so obviously you'll get junk values.
将传递一个指向位于数组后面的数据的指针,所以很明显你会得到垃圾值。
回答by HolyBlackCat
- Don't use
func(void)
in C++, it's some sort of deprecated. Usefunc()
instead. - If you have
float vett[10];
, thenvett[10]
is invalid. You must use onlyvett[0 .. 9]
float average(float array[10], float &average)
actually meansfloat average(float *array, float &average)
.
(Second form is more common way to declare pointer arguments.)
If you want to call it withvett
as argument, just useaverage(vett, media);
Names of an arrays, when used as pointers, are automatically converted to pointer to first element of an array. So hereaverage(vett, media);
vett
means&vett[0]
.
- 不要
func(void)
在 C++ 中使用,它在某种程度上已被弃用。使用func()
来代替。 - 如果有
float vett[10];
,则vett[10]
无效。您必须只使用vett[0 .. 9]
float average(float array[10], float &average)
其实是指float average(float *array, float &average)
。
(第二种形式是更常见的声明指针参数的方式。)
如果你想用vett
作为参数调用它,只需使用average(vett, media);
数组的名称,当用作指针时,会自动转换为指向数组第一个元素的指针。所以这里的average(vett, media);
vett
意思是&vett[0]
.
回答by alain
The number in the square brackets has two meanings:
方括号中的数字有两个含义:
- In a declaration like
float vett[10];
it is the size of the array - When not in a declaration, like
average(&vett[10],media);
it means the eleventh element of the array.
- 在类似的声明
float vett[10];
中是数组的大小 - 当不在声明中时,就像
average(&vett[10],media);
它意味着数组的第十一个元素。
average(&vett[10],media);
is passing the address of the eleventh element to the function. The function interprets it as the beginning of the array, which is wrong and causes undefined behaviour when the elements outside of the array are accessed.
average(&vett[10],media);
正在将第十一个元素的地址传递给函数。该函数将其解释为数组的开头,这是错误的,并且在访问数组外的元素时会导致未定义的行为。
Because you want to pass the whole array, you should use
因为要传递整个数组,所以应该使用
average(vett,media);
回答by Martin Milan
I'll add a little more to the help...
我会在帮助中添加更多内容...
Your average method has two return statements at the end of it. The final one (the one you will be wanting) will never be reached, because the method will return on the first one...
您的平均方法在其末尾有两个返回语句。永远不会到达最后一个(您将想要的那个),因为该方法将在第一个返回...
回答by Jonahatan pe?a
In the function instead of this
在函数中而不是 this
float average(float array[10], float &average){
you have to put this :
你必须把这个:
float average(float *array, float &average){