我正在学习C++,需要帮助我的代码.我不明白我的第二个函数中是如何得到编译器错误的.它几乎是第一个镜像,但我没有从第一个得到任何错误.
#include <iostream>
using namespace std;
// Function prototypes
int getLargest(int[], int);
int getSmallest(int[],int);
int main()
{
const int ARRAY_SIZE = 10;
int array[ARRAY_SIZE];
int count = 0;
cout << "Please enter 10 numbers into the array.\n\n";
for (count; count < ARRAY_SIZE; count++)
{
cout << "Number " << (count +1) << ": " ;
cin >> array[count];
}
// Call function to calculate largest number.
int largest = getLargest(array, ARRAY_SIZE);
// Display the largest number
cout << "The largest number is " << largest << endl;
// Call function to calculate smallest number.
int smallest = getSmallest(array, ARRAY_SIZE);
// Display the smallest number.
cout << "The smallest number is " << smallest << endl;
return 0;
}
//**********************************************************************
// Function definition getLargest.
// This function accepts an array argument and returns the largest element.
//**********************************************************************
int getLargest(int arrays[], int size1)
{
// Find the largest number in the array.
int large = arrays[0];
for (int count = 1; count < size1; count++)
{
if(arrays[count] > large)
large= arrays[count];
}
return large;
}
//**********************************************************************
// Function getSmallest definition.
// This function accepts an array argument and returns the smallest element.
//**********************************************************************
int getSmallest(int arrays[], int size2)
{
// Find the smallest number in the array.
int small = arrays[0];
for (count = 1; count < size2; count++)
{
if (arrays[count] < small)
small = arrays[count];
}
return small;
}
Run Code Online (Sandbox Code Playgroud)
这是我的错误:
777.cpp: In function ‘int getSmallest(int*, int)’:
777.cpp:62: error: overloaded function with no contextual type information
777.cpp:62: error: ‘size2’ cannot appear in a constant-expression
777.cpp:62: error: parse error in template argument list
777.cpp:62: error: could not convert ‘count<<expression error> >’ to ‘bool’
777.cpp:62: error: no post-increment operator for type
777.cpp:64: error: invalid types ‘int*[<unresolved overloaded function type>]’ for array subscript
777.cpp:65: error: invalid types ‘int*[<unresolved overloaded function type>]’ for array subscript
Run Code Online (Sandbox Code Playgroud)
在 getSmallest()
for (count = 1; count < size2; count++)
Run Code Online (Sandbox Code Playgroud)
你忘了申报 count
for (int count = 1; count < size2; count++)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
582 次 |
| 最近记录: |