C++ 错误 C2664:无法将参数 1 从“int”转换为“int []”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22150353/
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
error C2664: cannot convert parameter 1 from 'int' to 'int []'
提问by Amin Khormaei
#include <iostream>
using namespace std;
class amin
{
private:
const int length = 10;
int newArray[length];
int i;
public:
int deleteEvenNumber(int getArray[length])
{
for (i = 0 ; i < length ; i++)
{
if (getArray[i] % 2 == 0)
newArray[i] = getArray[i];
i++;
};
return newArray[length];
};
};
main:
主要的:
int main()
{
amin manipulateArrays;
int input , i = 0;
const int length = 10;
int mainArray[length];
cout<<"Please enter ten numbers :"<<endl;
for (i = 0 ; i < length ; i++)
{
cin>>input;
mainArray[i] = input;
i++;
};
manipulateArrays.deleteEvenNumber(mainArray[length]);
};
i got these two errors:
我收到了这两个错误:
error C2664: 'amin::deleteEvenNumber' : cannot convert parameter 1 from 'int' to 'int []'
IntelliSense: argument of type "int" is incompatible with parameter of type "int *"
错误 C2664:“amin::deleteEvenNumber”:无法将参数 1 从“int”转换为“int []”
IntelliSense:“int”类型的参数与“int *”类型的参数不兼容
please help and explain about my mistake to me.
请帮助并向我解释我的错误。
and please introduce a good tutorial for this problem or this title to me.
并请向我介绍有关此问题或此标题的好教程。
回答by herohuyongtao
Your function deleteEvenNumber()
requires an int []
(i.e. int
array), however you passed it an int
to it.
您的函数deleteEvenNumber()
需要一个int []
(即int
数组),但是您将它传递int
给了它。
manipulateArrays.deleteEvenNumber(mainArray[length]);
^^^^^^^^^^^^^^^^^
|
this is an 'int', not an 'int []'
To also pass the length
to the function, you may want to change your function to
要将 传递length
给函数,您可能需要将函数更改为
int deleteEvenNumber(int getArray[], int length)
And then call it like:
然后像这样调用它:
manipulateArrays.deleteEvenNumber(mainArray, length);
Alternatively, you can use vector<int> mainArray
instead, and then you can easily get its length by mainArray.size()
.
或者,您可以vector<int> mainArray
改为使用,然后您可以轻松地通过mainArray.size()
.
回答by Sam I am says Reinstate Monica
your function takes an int[]
(array) as a parameter
你的函数需要一个int[]
(数组)作为参数
int deleteEvenNumber(int getArray[length])
but you're passing an int
但你正在通过一个 int
manipulateArrays.deleteEvenNumber(mainArray[length]);