Reputation: 45
I am receiving this error
error: no match for 'operator<<' (operand types are 'std::ostream {aka std::basic_ostream<char>}' and 'void')
Here is my main.cpp
#include <iostream>
#include "WEATHERFORECASTER.h"
using namespace std;
int main()
{
WeatherForecaster yearData;
cout<<yearData.printDaysInData()<<endl;
}
Here is my 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(); //prints the unique dates in the data
void printForecastForDay(std::string);
void printFourDayForecast(std::string);
double calculateTotalPrecipitation();
void printLastDayItRained();
void printLastDayAboveTemperature(int); //argument is the temperature
void printTemperatureForecastDifference(std::string);
void printPredictedVsActualRainfall(int); //argument is days out, such as 1 = 1 day out, 2 = 2 days out, 3 = 3 days out
std::string getFirstDayInData();
std::string getLastDayInData();
protected:
private:
int arrayLength = 984;
int index;
ForecastDay yearData[984]; //data for each day
};
#endif // WEATHERFORECASTER_H
Here is my class file
#include "WEATHERFORECASTER.h"
#include <iostream>
#include <fstream>
#include <sstream>
using namespace std;
WeatherForecaster::WeatherForecaster()
{
//ctor
ifstream Fore; //Open up the file DATABOULDER.csv for weather data
Fore.open("DATABOULDER.csv");
if (Fore.fail()) //If it fails nothing will happen
{
}
else
{ //Otherwise open the file and begin sorting the data
string weather; //Create a string called weather
int lineIndex = 0; //Create a counter for the lines
while (getline(Fore, weather, '\n')) //Move through each line of the data by stopping at a character return
{ //Set the weather variable to that whole line of data
stringstream ss; //Create a stream using the weather string
ss << weather;
int weatherIndex = 0; //Create a new Index counter for each piece of data within the line
while (getline(ss, weather, ',')) //Go through the line and every comma store that as a weatherIndex
{
if (weatherIndex == 0) //Begin setting the pieces of the array beginning with the first index
{
string day = yearData[lineIndex].day; //If the index is 0 then set it as the .day extension
yearData[lineIndex].day = weather; //Set this equal to the weather variable in order to get the actual piece of data
}
else if (weatherIndex == 1) //If Index is 1 then this is the forecast day so use that extension
{
string forecastDay = yearData[lineIndex].forecastDay;
yearData[lineIndex].forecastDay = weather; //Set that equal to the weather variable to get actual data
}
else if (weatherIndex == 2) //If the Index is 2 then this is the high temp
{
istringstream convert(weather); //First convert weather so it can take ints
int highTemp = 0; //Create a highTemp int variable
string strHighTemp = ""; //Create a string to use with the string for converting the highTemp
strHighTemp = weather.substr(2, 2); //This allows for the H: to be removed and only a number or int
if (!(istringstream(strHighTemp) >> highTemp)) highTemp = 0;//Converting string highTemp to int highTemp and if it fails set highTemp to 0
yearData[lineIndex].highTemp = highTemp;
}
else if (weatherIndex == 3)
{
istringstream convert(weather); //Perform the exact same steps as previous for low temperatures
int lowTemp = 0;
string strLowTemp = "";
strLowTemp = weather.substr(2, 2);
if (!(istringstream(strLowTemp) >> lowTemp)) lowTemp = 0;
yearData[lineIndex].lowTemp = lowTemp;
}
else if (weatherIndex == 4) //If Index is 4 then that is humidity and we need to convert
{
istringstream convert(weather); //Convert weather to take ints
int humidity = 0; //Initialize a variable for humidity
if (!(istringstream(weather) >> humidity)) humidity = 0;//Convert string humidity to int humidity and if it fails set humidity to 0
yearData[lineIndex].humidity = humidity; //Set this index of the array to humidity variable type int
}
else if (weatherIndex == 5) //If Index is 5 then that is avgWind and we must convert
{
istringstream convert(weather); //Convert weather to take ints
int avgWind = 0; //Initialize variable for avgWind
if (!(istringstream(weather) >> avgWind)) avgWind = 0; //Convert string avgWind to int avgWind and if it fails set avgWind to 0
yearData[lineIndex].avgWind = avgWind; //Set this index of the array to the avgWind variable type int
}
else if (weatherIndex == 6) //If Index is 6 then it is the avg Wind Direction
{
yearData[lineIndex].avgWindDir = weather; //Set this index of the array to weather since it is a string
}
else if (weatherIndex == 7) //If Index is 7 then it is max Wind
{
istringstream convert(weather); //Convert weather to take ints
int maxWind = 0; //Initialize variable for maxWind
if (!(istringstream(weather) >> maxWind)) maxWind = 0;//Convert string maxWind to int maxWind and if it fails set maxWind to 0
yearData[lineIndex].maxWind = maxWind; //Set this index of the array to the maxWind variable type int
}
else if (weatherIndex == 8) //If Index is 8 then it is max Wind Direction
{
yearData[lineIndex].maxWindDir = weather; //Set equal to weather since it is a string
}
else if (weatherIndex == 9) //If Index is 9 then it is precipitation
{
istringstream convert(weather); //Convert weather to take doubles
double precip = 0; //Initialize variable for precipitation type double
if (!(istringstream(weather) >> precip)) precip = 0;//Convert string precip to int precip and if it fails set it to 0
yearData[lineIndex].precip = precip; //Set this index of the array to the precip variable of type double
}
weatherIndex++; //Increment each weatherIndex to get all lines
}
lineIndex++; //Increment each lineIndex to get all pieces from the lines
}
}
}
WeatherForecaster::~WeatherForecaster()
{
//dtor
}
void WeatherForecaster::printDaysInData()
{
//!display contents of array
for(int i = 0; i < arrayLength; i++)
{
cout<<yearData[i]<<endl;
}
}
I have done some research and found a couple of similar problems, but they make no sense to me or really don't match the problem I am having. They include:
error: no match for operator ==
C++ Error: 'no match for operator<...'
error: no match for ‘operator<<’ in ‘std::operator<<
Now I am working with a struct as seen in my header file, but I am populating the array from a file full of data. I am just confused as to what this error means in all of this since most of my code works for every other part. Is it to do with my header file or what is going on?
Any help or guidance would be appreciated thanks!!!
Upvotes: 2
Views: 24292
Reputation: 9397
You are displaying the values already inside your void
function:
void WeatherForecaster::printDaysInData()
{
//display contents of array
for(int i = 0; i < arrayLength; i++)
{
cout << yearData[i] << endl;
}
}
Therefore, in main()
you only have to call it, and that function will do its functionality:
int main()
{
WeatherForecaster yearData;
yearData.printDaysInData();
return 0;
}
You are getting that error because it is a void function.
Upvotes: 3