Dan*_*can 3 c++ overloading operator-overloading
#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;
}
Run Code Online (Sandbox Code Playgroud)
当我尝试使用g ++ 4.6.1编译此代码时:
"main.cpp:20:错误:'std :: istream&phonebook :: operator >>(std :: istream&,phonebook&)'必须只接受一个参数"
PS:这是非常愚蠢的事情......很明显:S.谢谢你.
operator >>作为成员函数,您不能重载流.任何被定义为成员函数的运算符都将其第一个参数作为对(const)Type的引用,其中Type是您的类名 - 在本例中为phonebook.
你需要改变
istream &phonebook::operator>>(istream &in, phonebook &book)
Run Code Online (Sandbox Code Playgroud)
至
istream & operator>>(istream &in, phonebook &book)
Run Code Online (Sandbox Code Playgroud)