如何让这个构造函数工作?

时间:2016-10-15 06:38:47

标签: c++

我坚持如何在头类中创建构造函数。我知道这就是我无法调用某些方法的原因。我真的不知道该怎么做,我试图复制我们的老师教我们的方式,但它似乎并没有像你看到的那样工作得太好。任何帮助表示赞赏。感谢。

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;

}

1 个答案:

答案 0 :(得分:0)

在这里删除所有无关的噪音似乎是(你的)问题之一:

class WeatherForecaster
{
    public:
        // you declare a constructor that does not take any variables
        WeatherForecaster();

    // ...
};

定义是否匹配?

// you define a constructor that takes three and a half million variables!!!
WeatherForecaster::WeatherForecaster(string d, string fd, int ht, int lt, int h, int aw, string awd, int mw, string mwd, double p){

    // ...    
}

您需要让构造函数声明和定义相互匹配,以及如何构造此类的对象。