没有命名空间std的友元重载操作符

friend overloaded operator without namespace std

本文关键字:友元 重载 操作符 std 命名空间      更新时间:2023-10-16

不知道有没有人能给我指出正确的方向。我有一个朋友提取操作符,如果我包括命名空间std;但如果我不这样做,就会失败。谁能给我点提示吗?

ostream& operator << (ostream &out, coins &value)

这也是一个好友函数,所以我在class.h文件中有这个函数(作为好友)在我的functions.h文件(作为原型)和functions.cpp文件(逻辑)中。

我试过了

std:: ostream&操作符……std:: ostream&运营商std:: & lt; & lt;(等)

但我就是看不出我错在哪里。我的编译器一直告诉我'ostream没有命名类型'

谢谢

存在于std命名空间中的是ostream,不要做std::<<(这甚至没有意义!)尽量少用鸟枪式的方法来编程;也就是说,不要随便尝试,直到成功为止。错误告诉您ostream(不合格)是问题所在,因此您必须首先解决该问题。

#include <iostream>
struct coins
{
    friend std::ostream& operator<<(std::ostream& sink, const coins& value);
};
std::ostream& operator<<(std::ostream& sink, const coins& value)
{
    sink << "doing coins output";
    return sink;
}
int main()
{
    coins c;
    std::cout << c << std::endl;
}
顺便说一下,

这是一个插入操作符,当你向流插入数据时。提取将是>>