C++超载>>运算符奇怪的编译错误
#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;
}
当我尝试使用 g++ 4.6.1 编译此代码时:
“main.cpp:20: error: 'std::istream& Phonebook::operator>>(std::istream&, Phonebook&)' 必须恰好取 1 PS:问这个问题
是相当愚蠢的……如此明显:S。不过还是谢谢你。
#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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您不能重载用于作为成员函数进行流式传输的
operator>>
。任何定义为成员函数的运算符都将其第一个参数作为对 (const) Type 的引用,其中 Type 是类名 - 在本例中为电话簿。你需要更改
为
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
friend
函数不是成员。事实上,它期望>>
运算符的左侧是一个电话簿
。将函数定义的第一行(在类之外)更改为:请注意,没有
phonebook::
因为它不是成员。A
friend
function isn't a member. As it is, it's expecting the left-hand side of the>>
operator to be aphonebook
. Change the first line of the function definition (outside the class) to this:Note there is no
phonebook::
because it's not a member.phonebook
没有名为oeartor>>
的方法您说过存在一个 global 函数,它是
phonebook 的友元函数
,因此,您应该从operator>>
的实现中删除phonebook::
phonebook
doesn't have a method calledopeartor>>
You stated that there exists a global function that is a friend of
phonebook
, and therefore, you should removephonebook::
from the implementation ofoperator>>
因为你声明了
friend istream &operator>>(istream &in, Phonebook &book);
所以这个operator>>>不是电话簿的会员功能。
引用自C++ 03标准
11.4 朋友
类的友元是一个函数或类,它不是该类的成员,但允许使用该类的私有和受保护成员名称。友元的名称不在类的范围内,并且不会使用成员访问运算符 (5.2.5) 调用友元,除非它是另一个类的成员。
因此删除
phonebook::
就可以了:because you declared
friend istream &operator>>(istream &in, phonebook &book);
So this operator>> is not a member function of phonebook.
Quote from C++ 03 standard
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. 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.
So remove the
phonebook::
would work:当您在类内声明函数为友元时,您可以在类内定义它或将其设为非成员函数。
When you declare function a friend inside the class, you either define it inside the class or make it a non-member function.