运算符<<必须只有一个参数 [英] operator << must take exactly one argument

查看:225
本文介绍了运算符<<必须只有一个参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ah

  #includelogic.h
...

A类
{
friend ostream& operator<<(ostream& amp; amp;);
...
};

logic.cpp

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



当我编译时,它说:


:ostream&逻辑::运算符<<<(std :: ostream& amp;)必须只有一个参数。


问题?问题是你在类中定义它,其中a)意味着第二个参数是隐式的(<$ p

$

this )和b)它不会做你想做的,即extend std :: ostream 。您必须将其定义为自由函数:

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


a.h

#include "logic.h"
...

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

logic.cpp

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

When i compile, it says:

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

What is the problem?

解决方案

The problem is that you define it inside the class, which a) means the second argument is implicit (this) and b) it will not do what you want it do, namely extend std::ostream. You have to define it as a free function:

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

这篇关于运算符&lt;&lt;必须只有一个参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆