I am bashing my head against the wall because of my code. Basically I need to create a program that reads a file and prints out what the specification asks for. I'm stuck on this part of the specification:
For 6 marks, create a file called analyze.cpp containing a program that:
• Opens sheffield.data and skips the header lines (getline will be handy for this)
• Reads data from the file into a vector of MonthData objects
• Uses the data stored in the vector to compute and display – The year and month with the lowest minimum temperature – The year with the highest total rainfall
In the sheffield.data file is this information in a nutshell:
yyyy mm tmax tmin af rain sun degC degC days mm hours
1930 1 8.1 2.4 6 120.5 54.2
1930 2 4.4 0.6 12 22.2 29.1
1930 3 8.1 2.1 9 76.2 88.2
I have 3 files. Data.cpp, Data.hpp and analyze.cpp
Data.hpp:
#ifndef DATA_HPP
#define DATA_HPP
#include <iostream>
#include <string>
#include <cmath>
class MonthData
{
friend std::istream& operator >> (std::istream&, MonthData&);
public:
//overload constructor
MonthData(double, int, double, double, int, double, double);
//Accessor functions
double getYear() const { return year; } //returns the year
int getMonth() const { return month; } //returns the month
double getMaximum() const { return temp_maximum; } //returns maximum temperature
double getMinimum() const { return temp_minimum; } //returns minimum temperature
int getFrost() const { return air_frost; } //returns air frost
double getRain() const { return rain; } //returns rainfall
double getSun() const { return sun; } //returns no of hours of sunshine
private:
double year;
double month;
double temp_maximum;
double temp_minimum;
int air_frost;
double rain;
double sun;
};
#endif
In my Data.cpp is this code:
#include "data.hpp"
#include <iostream>
using namespace std;
istream& operator >> (istream& in, MonthData& data)
{
in >> data.year >> data.year >> data.temp_maximum >> data.temp_minimum >> data.air_frost >> data.rain >> data.sun;
return in;
}
And finally in my analyze.cpp file is this code:
#include <istream>
#include <fstream>
#include <vector>
#include "data.hpp"
using namespace std;
int main()
{
MonthData data;
vector<MonthData> vectorData;
ifstream file ("sheffield.data");
string line;
int l_num = 0;
if (file.is_open()) {
while (getline(file, line))
if (l_num < 4) {
l_num += 1;
}
else {
file >> data;
vectorData.push_back(data);
}
float MinimumDeg = vectorData[0], getMinTemp();
int year = vectorData[0], getYear();
for (int a =o; a < vectorData.size(); a++)
{
MinimumDeg = VectorData[a], getMinTemp();
Year = VectorData[a], getYear();
}
cout << "Lowest year and month lowest rainfall: '\n'" << "Min Temp;" << MinimumDeg << "C '\n'" << "Year" << Year << endl;
return 0;
}
}
WHY AM I GETTING THESE ERRORS?
naveed@naveed-VirtualBox:~/Documents/cw$ g++ -Wall analyze.cpp -o analyze
analyze.cpp: In function ‘int main()’:
analyze.cpp:11:13: error: no matching function for call to ‘MonthData::MonthData()’
analyze.cpp:11:13: note: candidates are:
data.hpp:14:5: note: MonthData::MonthData(double, int, double, double, int, double, double)
data.hpp:14:5: note: candidate expects 7 arguments, 0 provided
data.hpp:8:7: note: MonthData::MonthData(const MonthData&)
data.hpp:8:7: note: candidate expects 1 argument, 0 provided
analyze.cpp:28:38: error: cannot convert ‘MonthData’ to ‘float’ in initialisation
analyze.cpp:29:30: error: cannot convert ‘MonthData’ to ‘int’ in initialisation
analyze.cpp:30:19: error: ‘o’ was not declared in this scope
analyze.cpp:30:42: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
analyze.cpp:32:22: error: ‘VectorData’ was not declared in this scope
analyze.cpp:33:9: error: ‘Year’ was not declared in this scope
analyze.cpp:36:115: error: ‘Year’ was not declared in this scope
analyze.cpp:29:11: warning: unused variable ‘year’ [-Wunused-variable]
Your MonthData class lacks a default constructor.
The code you wrote in main() constructs a MonthData with no arguments, that is the reason for the error above (and probably std::vector< MonthData > also needs a default constructor):
So provide a default constructor.
You are calling the default constructor here:
But you didn't implement one..
You created a constructor that receives parameters to read the file to variables and then instantiate your MonthData data(...);