我剛剛開始編寫代碼,並且正在學習有關數組的知識。我正在編寫一個程序,它接受一個數組列表,並告訴我第一個還是最後一個數字是2.爲此,我使用了一個函數。g ++編譯器無法識別我的功能
我的代碼如下所示:
#include <iostream>
using namespace std;
const int size = 6;
bool firstlast(int array[size]);
int main()
{
int array[size];
for (int index = 0; index < size; index++)
{
cout << "Enter value for array[" << index << "]\n";
cin >> array[index];
}
bool check = firstlast(array[size]);
if (check)
cout << "The array either starts or ends in 2!\n";
else
cout << "The array does not start or end with 2.\n";
return 0;
}
bool firstlast(int array[size])
{
if (array[0] == 2)
return true;
if (array[size - 1] == 2)
return true;
return false;
}
我在做什麼錯? 編譯器給我的錯誤:
candidate function not viable: no known conversion from 'int' to 'int *' for 1st argument; take the address of the argument with and
什麼是錯誤? – Ryan
我只是將它添加到問題 –
聲明應該是'bool firstlast(int array [size]);' - 它需要與函數定義一致並提供數組的類型。 –