我正在学习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;
}
以下是我的错误:
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
答案 0 :(得分:7)
在getSmallest()
for (count = 1; count < size2; count++)
您忘记声明count
for (int count = 1; count < size2; count++)
答案 1 :(得分:1)
这是你的问题:
for (count = 1; count < size2; count++)
在你的第二种方法中。你忘记了int:
for (int count = 1; count < size2; count++)
答案 2 :(得分:0)
我在第二个函数中得到计数错误,这是因为你没有定义它。使用此:
for (int count = 1; count < size2; count++) // int count; instead of count
{
if (arrays[count] < small)
small = arrays[count];
}