Overloading stream extraction operator
May 6, 2015 at 4:08am UTC
Hi everyone. I am new here. First time. First topic. First post. First everything.
Anyway, my question is about the stream extraction operator.
I have a header file, employee.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#ifndef EMPLOYEE_H
#define EMPLOYEE_H
#include<iostream>
using namespace std;
class employee{
string emp_name;
int sales;
public :
void display_highest();
friend istream& operator >>(istream& isObject, employee& x);
employee();
};
#endif
seems pretty fine. The error is in my employee.cpp file (implementation file). Says "No operator >> matches these operands"
here is my code in the employee.cpp file (snippet)
1 2 3
istream& operator >>(istream& isObject, employee& x){
isObject>>x.emp_name;
}
What am I doing wrong here?
May 6, 2015 at 4:30am UTC
Is there an #include <string>
lurking about somewhere?
May 6, 2015 at 7:41pm UTC
u must return istream object.
1 2 3 4
istream& operator >>(istream& isObject, employee& x){
isObject>>x.emp_name;
return isObject;
}
May 7, 2015 at 1:04am UTC
cire (5966) Thanks! I can't believe I missed that. I thought I needed to overload the extraction operator. That worked.
Topic archived. No new replies allowed.