Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
244 views
in Technique[技术] by (71.8m points)

c++ - How do I get this constructor to work?

I am stuck on how to make the constructor inside of the header class. I know that is why I can't call some methods. I really don't know what to do, I am trying to replicate how our teacher taught us, but it doesn't seem to be working too well as you can see. Any help is appreciated. Thanks.

Main.cpp File

#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, '
')){
            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 Header File

#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 Error: prototype for 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;

}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Removing all the irrelevant noise here seems to be (one of) your problem(s):

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

    // ...
};

Does the definition match?

// 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){

    // ...    
}

You need to get your constructor declarations and definitions to match each other and how you intend to construct objects of this class.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...