我收到此错误
error: no match for 'operator<<' (operand types are 'std::ostream {aka std::basic_ostream<char>}' and 'void')
这是我的main.cpp
#include <iostream>
#include "WEATHERFORECASTER.h"
using namespace std;
int main()
{
WeatherForecaster yearData;
cout<<yearData.printDaysInData()<<endl;
}
这是我的头文件
#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
这是我的班级文件
#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;
}
}
我做了一些研究,发现了几个类似的问题,但它们对我没有任何意义,或者真的不能解决我遇到的问题。 它们包括:
error: no match for operator ==
C++ Error: 'no match for operator<...'
error: no match for ‘operator<<’ in ‘std::operator<<
现在我正在处理我的头文件中看到的结构,但是我从一个充满数据的文件填充数组。我很困惑这个错误在所有这些中意味着什么,因为我的大部分代码都适用于其他所有部分。它与我的头文件或正在发生的事情有关吗?
任何帮助或指导都将不胜感激!!!
答案 0 :(得分:1)
您 显示void
功能中已有的值:
void WeatherForecaster::printDaysInData()
{
//display contents of array
for(int i = 0; i < arrayLength; i++)
{
cout << yearData[i] << endl;
}
}
因此,在main()
中您只需要调用它,该函数将执行功能:
int main()
{
WeatherForecaster yearData;
yearData.printDaysInData();
return 0;
}
您收到该错误,因为它是一个void函数。