关于 C++ 中自定义 I/O 运算符的问题?

发布于 2024-11-18 11:29:45 字数 1041 浏览 1 评论 0原文

我有以下代码:

 class Student {
 public:
     Student(){}
     void display() const{}
     friend istream& operator>>(istream& is, Student& s){return is;}
     friend ostream& operator<<(ostream& os, const Student& s){return os; }
 };
int main()
{
    Student st;
    cin >> st;
    cout << st;

    getch();
    return 0;
}

我自己尝试过省略 friend 关键字,使运算符成为 Student 类的成员函数,然后编译器会生成“binary 'operator > ;>'参数太多”。我读过一些文档说发生这种情况是因为所有成员函数总是接收一个隐式参数“this”(这就是为什么所有成员函数都可以访问私有变量)。 基于该解释,我尝试如下:

class Student {
 public:
     Student(){}
     void display() const{}
     istream& operator>>(istream& is){return is;}
     ostream& operator<<(ostream& os){return os; }
 };
int main()
{
    Student st;
    cin >> st;
    cout << st;

    getch();
    return 0;
}

并收到错误消息:“错误 C2679:二进制 '>>” : 找不到采用“Student”类型右侧操作数的运算符(或者没有可接受的转换)

谁能给我一个明确的解释?

I have the following piece of code:

 class Student {
 public:
     Student(){}
     void display() const{}
     friend istream& operator>>(istream& is, Student& s){return is;}
     friend ostream& operator<<(ostream& os, const Student& s){return os; }
 };
int main()
{
    Student st;
    cin >> st;
    cout << st;

    getch();
    return 0;
}

I have tried myself when omitting the friend keywords to make the operators become the member function of the Student class, then the compiler would produce "binary 'operator >>' has too many parameters". I have read some document saying that happened because all member functions always receive an implicit parameter "this" (that's why all member functions can access private variables).
Based on that explanation, I have tried as follows:

class Student {
 public:
     Student(){}
     void display() const{}
     istream& operator>>(istream& is){return is;}
     ostream& operator<<(ostream& os){return os; }
 };
int main()
{
    Student st;
    cin >> st;
    cout << st;

    getch();
    return 0;
}

And got the error message: "error C2679: binary '>>' : no operator found which takes a right-hand operand of type 'Student' (or there is no acceptable conversion)"

Can anyone give me a clear explanation, please?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

忘羡 2024-11-25 11:29:45

您不能说该函数是友元函数,然后将该函数内联包含在内。 friend关键字暗示该函数未在类中定义,但它可以访问该类的所有私有变量和受保护变量。将代码更改为:

class Student {
  public:
    Student(){}
    void display() const{}
    friend istream& operator>>(istream& is, Student& s);
    friend ostream& operator<<(ostream& os, const Student& s);
};

istream& operator >>(istream& is, Student& s) { return is; }
ostream& operator <<(ostream& os, const Student& s) { return os; }

查看 http://www.java2s.com/Code/另一个例子是 Cpp/Overload/Overloadstreamoperator.htm

与<<和>>,左侧操作数始终是文件流,因此您不能在实际类中重载它们(从技术上讲,它必须进入文件流类)。

You can't say that the function is a friend function, and then include the function in-line. The friend keyword implies that the function is not defined in the class, but it can access all the private and protected variables of the class. Change your code to:

class Student {
  public:
    Student(){}
    void display() const{}
    friend istream& operator>>(istream& is, Student& s);
    friend ostream& operator<<(ostream& os, const Student& s);
};

istream& operator >>(istream& is, Student& s) { return is; }
ostream& operator <<(ostream& os, const Student& s) { return os; }

Look at http://www.java2s.com/Code/Cpp/Overload/Overloadstreamoperator.htm for another example.

With << and >>, the left hand operand is always a file stream, so you cannot overload them within your actual class (it'd technically have to go in the file stream class).

北音执念 2024-11-25 11:29:45

我忘记了该运算符是在哪里定义的,但它要么是全局运算符>>,要么是属于流的运算符。

在 Student 中定义它是错误的地方。

I forget where that operator is defined, but it would either be the global operator>>, or the operator belonging to the stream.

Defining it in Student is the wrong place.

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