我有一个任务要收集十二个月的降雨。我的代码差不多完成了,但是我不断收到这些错误消息:
'rainfall'
:undeclared identifier
'month'
:undeclared identifier
'Stats::getAvg'
:function does not take 0 arguments
'rainfall'
:undeclared identifier
'month'
:undeclared identifier
'rainfall'
:undeclared identifier
'month'
:undeclared identifier
我不知道我在做什么错。这是我到目前为止的代码:
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
class Stats
{
private:
double value;
double total;
double avg;
double largest;
double smallest;
double rainfall[12];
int NUM_MONTHS;
public:
void setValue(int, double); // set function
double getTotal(double[], int); // get functions
double getAvg(double[], int);
double getLargest(double[], int);
double getSmallest(double[], int);
};
void Stats::setValue(int month, double rain)
{
rainfall[month] = rain;
}
double Stats::getTotal(double array[], int month)
{
double total = 0.0;
for (int count = 0; count < month; count++)
total += rainfall[count];
return total;
}
double Stats::getLargest(double array[], int month)
{
double largest = rainfall[0];
for (int count = 0; count < month; count++)
{
if (rainfall[count] > largest)
largest = rainfall[count];
}
return largest;
}
double Stats::getSmallest(double array[], int month)
{
double smallest = rainfall[0];
for (int count = 0; count < month; count++)
{
if (rainfall[count] < smallest)
smallest = rainfall[count];
}
return smallest;
}
// Function prototype
void rainReport(Stats);
int main()
{
Stats rainData; // Create an instance of the Stats class
// to manage rainfall data
double rain;
const int NUM_MONTHS = 12; // Number of elements the array can hold
string months[NUM_MONTHS] = {
"Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
};
for (int month = 0; month < NUM_MONTHS; month++)
{
cout << "Enter the rainfall (in inches) for month #";
cout << (month + 1) << ": ";
cin >> rain;
while (rain < 0)
{
cout << "Rainfall must be 0 or more. Please re-enter: ";
cin >> rain;
}
// Call class setValue function to store this month's rainfall
// value in the array.
rainData.setValue(month, rain);
}
// Call the rainReport function to produce a rain report.
// Pass it rainData, which is a Stats object.
rainReport(rainData);
return 0;
}
/************************************************************
* rainReport *
* Finds and returns the smallest value stored in the array.*
************************************************************/
void rainReport(Stats rainData)
{
// Display the total rainfall
cout << fixed << showpoint << setprecision(2) << endl;
cout << "Total rainfall for the year was ";
cout << setw(5) << rainData.getTotal(rainfall, month) << " inches." << endl;
// Display the average rainfall
cout << "Average rainfall for the year was ";
cout << setw(5) << rainData.getAvg() << " inches." << endl << endl;
// Display the largest & smallest amounts of monthly rain.
cout << "The largest amount of rainfall was ";
cout << setw(5) << rainData.getLargest(rainfall, month) << " inches.\n";
cout << "The smallest amount of rainfall was ";
cout << setw(5) << rainData.getSmallest(rainfall, month) << " inches.\n";
}
最佳答案
rainReport
不是您的班级统计信息的成员。因此,它不了解Stats中的所有内容。
cout << setw(5) << rainData.getTotal(rainfall, month) << " inches." << endl;
因此getTotal的参数会引发错误。降雨甚至无法访问(它是私人的),并且您无论如何都不使用该参数:
double getTotal (int); // new definition
double Stats::getTotal (int month)
{
double total = 0.0;
for (int count = 0; count < month; count ++)
total += rainfall[count];
return total;
}
并像
rainData.getTotal( 12)
这样称呼它关于c++ - 降雨统计C++,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20461242/