Why cant I use my cout overload operator?

New Programmer 1 Reputation point
2021-12-15T21:59:20.507+00:00

Hey guys. I am attempting to build a income calculator program strictly using classes, functions, and overloading the i/o stream operators. However my code keeps on generating an error saying ofStream cant be use as a fucntion but that is not what I am trying to do. Any help is appreciated thanks.

My code is attached.

include <string>

using namespace std;
class IncomeCalc{
const float rate=180.25;
public:
float income;
float hours_per_week;//hours you worked in a week
friend
ostream& operator<<(ostream& out, const IncomeCalc&income);
friend
istream& operator>>(istream&in,const IncomeCalc&hours_per_week);
};
void weekly(){
cout<<"Hello User!"<<"Here you can calculate your weekly income"<<endl;
cout<<"To do so, please insert your weekly hours worked."<<endl;

istream&operator>>(istream&in,const IncomeCalc&hours_per_week){
in>>hours_per_week;
return in;
}
ostream& operator<<(ostream& out, const IncomeCalc&income){
out<< "Wow!"<<" You have earned "<< hours_per_week<<" * "<<rate<<"Which is equal to :"<<endl;
out<<income<<endl;
return out;
}
};
int main(){
IncomeCalc calculate;
cout<<"Bye"<<endl;
return 0;
}

C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,441 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Minxin Yu 9,361 Reputation points Microsoft Vendor
    2021-12-16T02:40:28.22+00:00

    Hi, @New Programmer

    There are several errors in the snippet that need to be corrected:

    1. Do not define another function in one function definition.
    2. There is no need to use const in istream& operator>>. For your reference: Overloading the >> and Overloading the <<
    3. Income in ostream& operator<<(ostream& out, const IncomeCalc&income) is a class, not a member.
    4. Do not use uninitialized members, you should initialize them in the constructor before using them.

    Below is the modified snippet for your reference:

    #include <string>  
    #include<iostream>  
    using namespace std;  
    class IncomeCalc {  
    	const float rate = 180.25;  
    public:  
    	float income=0;     //  modified =0  
    	float hours_per_week=0;//hours you worked in a week  
    	friend  
    		ostream& operator<<(ostream& out, const IncomeCalc& incomeCalc);  
    	friend  
    		istream& operator>>(istream& in, IncomeCalc& incomeCalc);  
    };  
    void weekly() {  
    	cout << "Hello User!" << "Here you can calculate your weekly income" << endl;  
    	cout << "To do so, please insert your weekly hours worked." << endl;  
    }  //modified { }  
    	istream& operator>>(istream & in,  IncomeCalc & incomeCalc) {      // removed const  
    		in >> incomeCalc.hours_per_week;    //modified  incomeCalc.hours_per_week    
    		return in;  
    	}  
    	ostream& operator<<(ostream & out, const IncomeCalc & incomeCalc) {  
    		out << "Wow!" << " You have earned " << incomeCalc.hours_per_week << " * " << incomeCalc.rate << "Which is equal to :" << endl;  //modified  incomeCalc.  
    		out << incomeCalc.income << endl;  
    		return out;  
    	}  
    
    int main() {  
    	IncomeCalc calculate;  
    	cout << "Bye" << endl;  
    	return 0;  
    }  
    

    Best regards,

    Minxin Yu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our [documentation][3] to enable e-mail notifications if you want to receive the related email notification for this thread.