gpt4 book ai didi

C++ 错误 : no match for 'operator<<' (operand types are

转载 作者:行者123 更新时间:2023-11-30 05:21:41 34 4
gpt4 key购买 nike

我收到这个错误

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

这是我的类(class)文件

#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<<

现在我正在使用头文件中显示的结构,但我正在从充满数据的文件中填充数组。我只是对这个错误在所有这些中意味着什么感到困惑,因为我的大部分代码都适用于所有其他部分。是我头文件的问题还是怎么回事?

如有任何帮助或指导,我们将不胜感激!!!

最佳答案

正在显示您的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 函数。

关于C++ 错误 : no match for 'operator<<' (operand types are,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39984346/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com