#include <iostream>
#include <string>
using namespace std;
class phonebook
{
string name;
string prefix;
public:
phonebook(string &name, string &prefix)
{
this->name = name;
this->prefix = prefix;
}
friend istream &operator>>(istream &in, phonebook &book);
};
istream &phonebook::operator>>(istream &in, phonebook &book)
{
in >> book.name >> book.prefix;
return in;
}
int main()
{
return 0;
}
When I try to compile this code using g++ 4.6.1:
“main.cpp:20: error: ‘std::istream& phonebook::operator>>(std::istream&, phonebook&)’ must take exactly one argument”
PS: It was pretty dumb thing to ask… So obvious :S. Thank you though.
You cannot overload
operator >>for streaming as a member function. Any operator that is defined as a member function takes its first argument as a reference to (const) Type, where Type is your class name – in this case, phonebook.You need to change
to