[英]C++ overloading >> operator strange compile error
#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: 当我尝试使用g ++ 4.6.1编译此代码时:
"main.cpp:20: error: 'std::istream& phonebook::operator>>(std::istream&, phonebook&)' must take exactly one argument" “main.cpp:20:错误:'std :: istream&phonebook :: operator >>(std :: istream&,phonebook&)'必须只接受一个参数”
PS: It was pretty dumb thing to ask... So obvious :S. PS:这是非常愚蠢的事情......很明显:S。 Thank you though. 谢谢你。
You cannot overload operator >>
for streaming as a member function. 您不能重载operator >>
for streaming作为成员函数。 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. 任何被定义为成员函数的运算符都将其第一个参数作为对(const)Type的引用,其中Type是您的类名 - 在本例中为phonebook。
You need to change 你需要改变
istream &phonebook::operator>>(istream &in, phonebook &book)
to 至
istream & operator>>(istream &in, phonebook &book)
A friend
function isn't a member. friend
功能不是会员。 As it is, it's expecting the left-hand side of the >>
operator to be a phonebook
. 实际上,它期望>>
运算符的左侧是phonebook
。 Change the first line of the function definition (outside the class) to this: 将函数定义的第一行(类外)更改为:
istream &operator>>(istream &in, phonebook &book)
Note there is no phonebook::
because it's not a member. 请注意,没有phonebook::
因为它不是会员。
phonebook
doesn't have a method called opeartor>>
phonebook
没有一个叫做opeartor>>
的方法
You stated that there exists a global function that is a friend of phonebook
, and therefore, you should remove phonebook::
from the implementation of operator>>
您声明存在一个全局函数,它是phonebook
的朋友,因此,您应该从operator>>
的实现中删除phonebook::
because you declared friend istream &operator>>(istream &in, phonebook &book);
因为你宣布了friend istream &operator>>(istream &in, phonebook &book);
So this operator>> is not a member function of phonebook. 所以这个运营商>>不是电话簿的成员功能。
Quote from C++ 03 standard 从C ++ 03标准引用
11.4 Friends A friend of a class is a function or class that is not a member of the class but is permitted to use the private and protected member names from the class. 11.4朋友类的朋友是一个函数或类,它不是类的成员,但允许使用类中的私有和受保护的成员名。 The name of a friend is not in the scope of the class, and the friend is not called with the member access operators (5.2.5) unless it is a member of another class. 朋友的名称不在类的范围内,并且除非成员是另一个类的成员,否则不会使用成员访问运算符(5.2.5)调用该朋友。
So remove the phonebook::
would work: 所以删除phonebook::
会工作:
istream& operator>>(istream &in, phonebook &book)
{
in >> book.name >> book.prefix;
return in;
}
When you declare function a friend inside the class, you either define it inside the class or make it a non-member function. 当你在类中声明一个朋友的函数时,你要么在类中定义它,要么使它成为非成员函数。
istream & operator>>(istream &in, phonebook &book)
{
in >> book.name >> book.prefix;
return in;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.