C++超载>>运算符奇怪的编译错误

发布于 2024-12-04 16:05:35 字数 744 浏览 0 评论 0原文

#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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

我还不会笑 2024-12-11 16:05:35

您不能重载用于作为成员函数进行流式传输的operator>>。任何定义为成员函数的运算符都将其第一个参数作为对 (const) Type 的引用,其中 Type 是类名 - 在本例中为电话簿。

你需要更改

istream &phonebook::operator>>(istream &in, phonebook &book)

istream & operator>>(istream &in, phonebook &book)

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

istream &phonebook::operator>>(istream &in, phonebook &book)

to

istream & operator>>(istream &in, phonebook &book)
挽容 2024-12-11 16:05:35

friend 函数不是成员。事实上,它期望 >> 运算符的左侧是一个电话簿。将函数定义的第一行(在类之外)更改为:

istream &operator>>(istream &in, phonebook &book)

请注意,没有 phonebook:: 因为它不是成员。

A friend function isn't a member. As it is, it's expecting the left-hand side of the >> operator to be a 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.

一指流沙 2024-12-11 16:05:35

phonebook 没有名为 oeartor>> 的方法

您说过存在一个 global 函数,它是 phonebook 的友元函数,因此,您应该从 operator>> 的实现中删除 phonebook::

phonebook doesn't have a method called 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>>

兰花执着 2024-12-11 16:05:35

因为你声明了friend istream &operator>>(istream &in, Phonebook &book);

所以这个operator>>>不是电话簿的会员功能。

引用自C++ 03标准

11.4 朋友
类的友元是一个函数或类,它不是该类的成员,但允许使用该类的私有和受保护成员名称。友元的名称不在类的范围内,并且不会使用成员访问运算符 (5.2.5) 调用友元,除非它是另一个类的成员。

因此删除 phonebook:: 就可以了:

istream& operator>>(istream &in, phonebook &book)
{
    in >> book.name >> book.prefix;
    return in;
}

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:

istream& operator>>(istream &in, phonebook &book)
{
    in >> book.name >> book.prefix;
    return in;
}
青朷 2024-12-11 16:05:35

当您在类内声明函数为友元时,您可以在类内定义它或将其设为非成员函数。

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文