【问题标题】:c++ error: no match for 'operator<<' (operand types arec++ 错误:'operator<<' 不匹配(操作数类型为
【发布时间】:2016-10-11 18:36:51
【问题描述】:

我收到此错误

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

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

任何帮助或指导将不胜感激!!!

【问题讨论】:

  • 由于您的函数是 void 并且它确实打印了值,所以只需在没有 cout 的情况下调用它
  • 哇,这个问题有一百万个重复。您是否考虑过实际上首先进行研究?
  • 我做了研究,如帖子所示。对不起,我不明白他们想说什么。我实际上尝试了他们的许多响应,但仍然无法使代码正常工作。那我该怎么办?接受它,永远不要问。
  • 为什么我不能问这个问题?我是一个初学者程序员,几乎没有上过我的第一堂编程课的经验。我研究了这个问题。发现了一些类似的问题,但无法理解其中一些人在说什么,但仍然试图将其实现到我的代码中。我没有得到敌意,抱歉我什至问了。
  • 对于您链接的所有参考资料,很难理解您从那里的答案中没有得到什么。很抱歉听起来很粗鲁(不是故意的),但是您能否将您的代码归结为 minimal reproducible example 以重现您的问题,并说明您不明白的地方?

标签: c++ class compiler-errors codeblocks


【解决方案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 函数。

【讨论】:

  • 好的,我注意到我在那里做错了,但现在我得到了与你第一次提到的那段代码相同的错误
  • 该错误与&lt;&lt; 运算符有关。删除&lt;&lt; 时怎么会出现同样的错误?
  • 我的 void 函数与 cout
  • 是的,因为你的数组有对象。您无法按原样显示对象。您究竟想从该对象中显示什么?您需要指定 -> 例如:cout &lt;&lt; yearData[i].???? &lt;&lt; endl;
  • 是的,我今天早些时候发现了这一点。感谢您的帮助,我是一个普通的菜鸟。
猜你喜欢
  • 2019-09-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-29
  • 2023-04-07
  • 1970-01-01
  • 1970-01-01
  • 2020-09-07
相关资源
最近更新 更多