C ++问题“无法将参数1从'int [5]'转换为'int'”

时间:2016-04-18 02:22:20

标签: c++

c ++的新手,我想写一个这样的程序:

  

void fillArray(int ar [],int size,int inc);

     

该函数假定第0个元素ar [0]已经填充了一些值,并从ar [1] on填充剩余的槽,前面的元素加上'inc'的值。例如,当ar [0]填充5时,ar [1]应为8,ar [2]应为11,依此类推。

     

我不断收到错误“无法将参数1从'int [5]'转换为'int'”。我该如何解决这个问题?如何完成我的代码?结果如下:

     

输入ar1:4 3

的第一个值和增量      

输入ar2的第一个值和增量:5 2

     

(1)ar1:   4 7 10 13 16

以下是我的代码:

#include<iostream>
#include <cstdlib>   // to use rand(), srand() and RAND_MAX
#include <ctime>     // to use the library function time()
using namespace std;

//prototype
void fillArray(int,int,int);
void printArray(int,int);

int main()
{
srand(time(0));  // initialize random number generator

int x1,y1,x2,y2;
cout << "Enter the first value and increment for ar1: ";
cin >> x1, y1;
cout << "Enter the first value and increment for ar2: ";
cin >> x2, y2;

const int size = 5;
int ar1[size], ar2[size];

ar1[0] = x1, ar2[0] = x2;

cout << "(1) ar1:\n";
fillArray(ar1,size,y1);


system("pause");
return 0;

}
void fillArray(int ar[], int size, int inc)
{
for (int i = 1; i < size; i++)
{
    ar[i] = ar[0] + inc;
    cout << ar[size];
}
}

谢谢!

1 个答案:

答案 0 :(得分:3)

这是你最初声明的原型:

void fillArray(int,int,int);

当橡胶碰到道路时,这是你的实际功能:

void fillArray(int ar[], int size, int inc)

你看到了问题吗?

原型必须与函数的签名完全匹配。您应该将原型编写为:

void fillArray(int [],int,int);