c++ - 运算符 << 必须只接受一个参数

标签 c++ operator-overloading iostream

啊。

#include "logic.h"
...

class A
{
friend ostream& operator<<(ostream&, A&);
...
};

逻辑.cpp

#include "a.h"
...
ostream& logic::operator<<(ostream& os, A& a)
{
...
}
...

当我编译时,它说:

std::ostream& logic::operator<<(std::ostream&, A&)' must take exactly one argument.

有什么问题?

最佳答案

问题是你在类中定义它,

a) 表示第二个参数是隐式的 (this) 和

b) 它不会做你想做的事,即扩展std::ostream

你必须把它定义为一个自由函数:

class A { /* ... */ };
std::ostream& operator<<(std::ostream&, const A& a);

关于c++ - 运算符 << 必须只接受一个参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10744787/

相关文章:

c++ - Boost ASIO 连接失败

c++ - shared_ptr 的自定义删除器的附加参数

c# - 为值类型实现 operator++ 的正确方法是什么?

c++ - 在数字前面添加 0

c++ operator<<的多重定义

c++ - 在基类中,如何定义一个容器来包含可以是派生类的任何函数的函数对象?

c++ - 无法将 int 类型转换为时间类型(我的类(class)类型)

C++如何使用链表添加多项式

c++ - 将浮点 ostream 打印限制为一定长度?

c++ - 无法在 QML ListView 中调用 Qt C++ 方法