-2
我被困在如何使頭部類的構造函數。我知道這就是爲什麼我不能調用某些方法。我真的不知道該怎麼做,我試圖複製我們的老師教我們的方式,但它看起來並不像你所看到的那樣工作得很好。任何幫助表示讚賞。謝謝。如何讓這個構造函數工作?
Main.cpp的文件
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include "WeatherForecaster.h"
using namespace std;
int main() {
ifstream ifs;
ifs.open("boulderData.txt");
int counter = 0;
ForecastDay yearData[984];
if(ifs.fail()){
cout<<"File failed to open."<<endl;
}else{
string line;
while(getline(ifs, line, '\n')){
stringstream ss;
ss<<line;
string date1;
string date2;
getline(ss, date1, ',');
getline(ss, date2, ',');
string item;
yearData[counter].day = date1;
yearData[counter].forecastDay = date2;
getline(ss, item, ',');
size_t found = item.find(":");
string s;
s = item.substr(found+1, item.length()-1);
yearData[counter].highTemp = stoi(s);
getline(ss, item, ',');
found = item.find(":");
s = item.substr(found+1, item.length()-1);
yearData[counter].lowTemp = stoi(s);
getline(ss, item, ',');
yearData[counter].humidity = stoi(item);
getline(ss, item, ',');
yearData[counter].avgWind = stoi(item);
getline(ss, item, ',');
yearData[counter].avgWindDir = item;
getline(ss, item, ',');
yearData[counter].maxWind = stoi(item);
getline(ss, item, ',');
yearData[counter].maxWindDir = item;
getline(ss, item, ',');
yearData[counter].precip = stod(item);
counter++;
}
}
WeatherForecaster wf;
ForecastDay fd;
// wf.printLastDayItRained(fd);
double totalRain = wf.calculateTotalPrecipitation();
cout << "Total rainfall: " << totalRain << endl;
/*cout<< "Enter a date:" << endl;
string date;
getline(cin, date);
wf.printForecastForDay(date);*/
}
WeatherForecast頭文件
#ifndef WEATHERFORECASTER_H
#define WEATHERFORECASTER_H
#include <iostream>
struct ForecastDay{
std::string day;
std::string forecastDay;
int highTemp;
int lowTemp;
int humidity;
int avgWind;
std::string avgWindDir;
int maxWind;
std::string maxWindDir;
double precip;
};
class WeatherForecaster
{
public:
WeatherForecaster();
~WeatherForecaster();
void addDayToData(ForecastDay);
void printDaysInData();
void printForecastForDay(std::string);
void printFourDayForecast(std::string);
double calculateTotalPrecipitation();
void printLastDayItRained();
void printLastDayAboveTemperature(int);
void printTemperatureForecastDifference(std::string);
void printPredictedVsActualRainfall(int);
std::string getFirstDayInData();
std::string getLastDayInData();
protected:
private:
int arrayLength = 984;
int index;
ForecastDay yearData[984];
};
#endif // WEATHERFORECASTER_H
WeatherForecast.cpp錯誤:這裏原型WeatherForecast :: WeatherForecast
#include "WeatherForecaster.h"
#include <iostream>
#include <string>
using namespace std;
WeatherForecaster::WeatherForecaster(string d, string fd, int ht, int lt, int h, int aw, string awd, int mw, string mwd, double p){
ForecastDay::day = d;
ForecastDay::forecastDay = fd;
ForecastDay::highTemp = ht;
ForecastDay::lowTemp = lt;
ForecastDay::humidity = h;
ForecastDay::avgWind = aw;
ForecastDay::avgWindDir = awd;
ForecastDay::maxWind = mw;
ForecastDay::maxWindDir = mwd;
ForecastDay::precip = p;
}
WeatherForecaster::~WeatherForecaster(){
}
/*void addDayToData(ForecastDay yearData[]) {
for(int i = 0; i < 984; i++) {
if()
}
}*/
void printDaysInData(ForecastDay yearData[]) {
for(int i = 0; i < 984; i++) {
if(yearData[i].day == yearData[i].forecastDay) {
cout << yearData[i].day << endl;
}
}
}
void printForecastForDay(ForecastDay yearData[], string date) {
for(int i = 0; i < 984; i++) {
if(date == yearData[i].day && date == yearData[i].forecastDay)
cout << "" << endl;
cout << "Forecast for " << yearData[i].day << ": " << endl;
cout << "H: " << yearData[i].highTemp << endl;
cout << "L: " << yearData[i].lowTemp << endl;
cout << "Humidity: " << yearData[i].humidity << endl;
cout << "Avg Wind: " << yearData[i].avgWind << endl;
cout << "Avg Wind Direction: " << yearData[i].avgWindDir << endl;
cout << "Max Wind: " << yearData[i].maxWind << endl;
cout << "Max Wind Direction: " << yearData[i].maxWindDir << endl;
cout << "Precipitation: " << yearData[i].precip << endl;
}
}
void printFourDayForecast(string) {
}
double calculateTotalPrecipitation(ForecastDay yearData[]) {
double totalRain = 0;
for(int i = 0; i < 984; i++) {
totalRain = totalRain + yearData[i].precip;
}
return totalRain;
}
void printLastDayItRained(ForecastDay yearData[]) {
string lastRained;
for(int i = 0; i < 984; i++) {
if(yearData[i].precip > 0) {
lastRained = yearData[i].day;
cout << lastRained << endl;
}
}
}
void printLastDayAboveTemperature(ForecastDay yearData[], int avgTemp) {
string lastTemp;
for(int i = 0; i < 984; i++) {
if(yearData[i].day == yearData[i].forecastDay) {
if(yearData[i].highTemp > avgTemp) {
lastTemp = yearData[i].day;
}
}
}
cout << lastTemp << endl;
}
void printTemperatureForecastDifference(ForecastDay yearData[], string date) {
for(int i = 0; i < 984; i++) {
if(yearData[i].forecastDay == date) {
cout << "Forecast for " << yearData[i].forecastDay << " issued on " << yearData[i].day << endl;
cout << "H: " << yearData[i].highTemp << endl;
cout << "L: " << yearData[i].lowTemp << endl;
}
if(yearData[i].forecastDay == date && yearData[i].day == date) {
cout << "Actual forecast for " << yearData[i].day << endl;
cout << "H: " << yearData[i].highTemp << endl;
cout << "L: " << yearData[i].lowTemp << endl;
}
}
}
void printPredictedVsActualRainfall(int) {
}
string getFirstDayInData(ForecastDay yearData[]) {
string date;
for(int i = 0; i < 984; i++) {
if(yearData[i].day == yearData[i].forecastDay) {
date = yearData[i].day;
}
}
return date;
}
string getLastDayInData(ForecastDay yearData[]) {
string date;
string currentDate;
for(int i = 0; i < 984; i++) {
if(yearData[i].day == yearData[i].forecastDay) {
date = yearData[i].day;
}
}
return date;
}
這個非常相同的問題[這裏已經問過](http://stackoverflow.com/questions/40055028/need-help-identifying-issue-with-source-code-c-header-struct-class-complica) 。 – PaulMcKenzie
我在做什麼錯你從那裏解釋?我覺得我正在使構造方法成爲正確的方式。 – PlagueKail
請閱讀該主題上的評論。構造函數在這裏聲明在哪裏? 'WeatherForecaster(string d,string fd,int ht,int lt,int h,int aw,string awd,int mw,string mwd,double p);'?你不能只編寫不存在的函數。你所聲明的是一個不帶任何參數的構造函數。 – PaulMcKenzie