c++ error: no match for 'operator<<' (operand types are
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.
Joseph McCarthy
Updated on June 04, 2022Comments
-
Joseph McCarthy almost 2 years
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!!!
-
Khalil Khalaf over 7 yearsSince your function is a void and it does print the values, then just call it without the
cout
-
πάντα ῥεῖ over 7 yearsAww, a felt million duplicates for that question. Did you consider researching first actually?
-
Joseph McCarthy over 7 yearsI did research as shown in the post. I am sorry I don't understand what they were trying to say. I actually tried many of their responses and still couldn't get the code to work. So what was I supposed to do? Suck it up and never ask.
-
Joseph McCarthy over 7 yearsWhy am I not allowed to ask the question? I am a beginner programmer with little experience taking my first programming class. I researched the problem. Found some similar issues, but couldnt understand what some of them were saying, but still tried to implement it into my code. I don't get the hostility, sorry I even asked.
-
πάντα ῥεῖ over 7 yearsAs for all the references you linked it's hard to understand what you didn't get from the answers there. Sorry to sound rude (wasn't intentional), but can you boil down your code to a minimal reproducible example that reproduces your problem, and make it clear what you don't understand please?
-
Joseph McCarthy over 7 yearsI tried googling the exact error, but could only find a few examples similar to my error. I simply don't understand some of these complex answers since either some are way out of my league of knowledge or I just simply can't figure out what is going on with their code compared to mine. FirstStep below answered my question and helped me solve it, but now it produces the same error in a different location. Its these things that I personally cannot understand with my minimal knowledge. I will try to better research this problem and come back with a clear problem.
-
-
Joseph McCarthy over 7 yearsOk I notice what I was doing wrong there, but now I get the same error for that piece of code you first mentioned the
-
Khalil Khalaf over 7 yearsThe error is about the
<<
operator. How could you get the same error when you remove the<<
? -
Joseph McCarthy over 7 yearsMy void function with the cout << yearData[i]<< endl; is now producing that same error.
-
Khalil Khalaf over 7 yearsYes because you array has objects. You can't display the object as is. What do you exactly want to display out of that object? You need to specify -> For ex:
cout << yearData[i].???? << endl;
-
Joseph McCarthy over 7 yearsYeah I figured that out earlier today. Thanks for the help I was being a plain noob.
-
Khalil Khalaf over 7 yearsCool. Glad we were able to help :) please mark / upvote this answer if it answers your question.