I have the following overloaded extraction operator function where I need to read values into an array. I keep on getting compile errors when I uncomment the: dayReport.dayOfMonth = dayP part. Could anyone point me to where I am going wrong. What I am trying to do is to read values from a file into an array.

[

istream& operator >> (istream& fin, weatherReport dayReport)
{ 
    int dayP, highP, lowP, rainP, snowP, i;
    
    i = 0; 
    cout << "Reading data from file..." << endl;
    
	cout << left << setw(13)<< "Day #" 
		 << setw(13) << "High Temp" 
         << setw(13) << "Low Temp" 
         << setw(13) << "Rain"
         << setw(13) << "Snow" << endl 
		 << fixed << showpoint;  
		 
	while(fin >> dayP >> highP >> lowP >> rainP >> snowP)
	{
		cout << left << setw(13) << dayP << setw(13) << highP
			 << setw(13) << lowP << setw(13) << rainP << setw(13)  
			 << snowP << setw(13) << endl; 	

       // dayReport[i].dayOfMonth = dayP;
        i++;
    }

]

Recommended Answers

All 7 Replies

The problem is the dayReport is only 1 class past to the function, not an array of classes:

istream& operator >> (istream& fin, weatherReport dayReport)

So you'll have to adjust the function to take in an array of classes.

Thank's for the reply:

When I add the [] and try to compile it I get a compilation error stading that there is no match for operator >>.

Any help to show me where I am going wrong would be greatly appreciated.

In my main function I declare the following:

[

int main()
{
    weatherReport dayReport[30], monthly;
    ifstream fin;
   
    fin.open("weather.dat");

    if (fin.fail())
    { 
        cout << "Input data file failed to open" << endl;
        exit(1);
    }
    
    fin >> monthly;
    fin.close(); 
        
    return 0;
}

class weatherReport
{
    public:
        friend istream& operator>>(istream& fin, weatherReport dayReport[]);
        friend ostream& operator<<(ostream& fout, const weatherReport& monthlyP);    
        weatherReport();
        ~weatherReport();         
        weatherReport(int Pday, int Phigh, int Plow, int Prain, int Psnow);
        void monthEnd(); 
    
    private:
        int dayOfMonth;
        int highTemp;
        int lowTemp;
        int amountRain;
        int amountSnow;
};

]

Did you change the definition of stream& operator>> as well as the declaration?

Also, when posting code, please use code tags

Nick thanks here is my entire code, I have searched all over the web as to how to use arrays with classes but can't find a decent example to follow. Appreaciate any help.

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <iomanip>
using namespace std;

class weatherReport
{
    public:
        friend istream& operator>>(istream& fin, weatherReport dayReport[]);
        friend ostream& operator<<(ostream& fout, const weatherReport& monthlyP);    
        weatherReport();
        ~weatherReport();         
        weatherReport(int Pday, int Phigh, int Plow, int Prain, int Psnow);
        void monthEnd(); 
    
    private:
        int dayOfMonth;
        int highTemp;
        int lowTemp;
        int amountRain;
        int amountSnow;
};

weatherReport::weatherReport(int dayP, int highP, int lowP, int rainP, int snowP)
{
	dayOfMonth = dayP;
	highTemp = highP;
	lowTemp = lowP;
	amountRain = rainP;
	amountSnow = snowP;
}

weatherReport::weatherReport() : dayOfMonth(0), highTemp(0), lowTemp(0), amountRain(0), amountSnow(0)
{
	
}

istream& operator >> (istream& fin, weatherReport dayReport[])
{ 
    int dayP, highP, lowP, rainP, snowP, i;
    
    i = 0; 
    cout << "Reading data from file..." << endl;
    
	cout << left << setw(13)<< "Day #" 
		 << setw(13) << "High Temp" 
         << setw(13) << "Low Temp" 
         << setw(13) << "Rain"
         << setw(13) << "Snow" << endl 
		 << fixed << showpoint;  
		 
	while(fin >> dayP >> highP >> lowP >> rainP >> snowP)
	{
		cout << left << setw(13) << dayP << setw(13) << highP
			 << setw(13) << lowP << setw(13) << rainP << setw(13)  
			 << snowP << setw(13) << endl; 	

        dayReport[i].dayOfMonth = dayP;
        i++;
    }  		 
    
    cout << "Read values into array!" << endl;
    
    return fin;   
}

ostream& operator<<(ostream& fout, const weatherReport& monthlyP)
{

}

weatherReport::~weatherReport()
{

}

int main()
{
    weatherReport dayReport[30], monthly;
    ifstream fin;
   
    fin.open("weather.dat");

    if (fin.fail())
    { 
        cout << "Input data file failed to open" << endl;
        exit(1);
    }
    
    fin >> monthly;
    fin.close(); 
        
    return 0;
}

I am new to C++ and am struggling to use array of classes in the overloaded extraction operator.

I'm not sure what your program is supposed to do, but this line: fin >> monthly; is wrong. You now only have an implementation of an istream operator which takes in an array of weather reports. So changing it to : fin >> dayReport; would solve the problem.

If you don't want to send an array of classes at that point in your program, then you should overload the >> operator twice. One to accept a weatherreportclass, and one to accept an array of classes.

Niek thanks soz about that, when do you use the & in the ovverloaded extraction operator? I need to complete an assignment and it asks that I use the overloaded extraction operator >> to read a month's data into an array of 30 objects.

I made the changes to my code and it is not giving me the error anymore thanks alot.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.