超载<< C++ 中的运算符
我想超载<< Line 类中的运算符,因此我可以使用 cout 打印一个对象,如下所示:
cout << myLineObject << endl;
但这不起作用:
class Line{
public:
float m;
float b;
string operator << (Line &line){return ("y = " + line.m + "x + " + line.b);};
};
我得到:
Invalid operands of types 'const char [5]' and 'float' to binary 'operator+'
我也尝试过使用 stringstream
但出现更多错误。这样做的正确方法是什么?
谢谢 ;)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
讨论重载
<<
的地方都列出了正确的方法,而您几乎错过了所有内容。标准声明是
ostream & operator<<(ostream & s, const & Line l);
它不能是成员函数,并且需要返回对ostream
的引用,以便您可以链接<<
正常。在您的情况下,定义类似于
注意您返回传入的ostream,并使用
<<
运算符而不是使用<来打印您喜欢的内容代码>+运算符。如果你遵循这个,这非常简单形式。
在这种情况下,数据成员是公共的(这通常不是一个好主意),
所以不存在访问问题。如果您需要获取无法访问的值(因为
它们是
私有
并且不会在公共接口中公开),您需要声明运算符成为类定义中的
friend
。The correct way is listed everywhere overloading
<<
is discussed, and you've managed to miss pretty much all of it.The standard declaration is
ostream & operator<<(ostream & s, const & Line l);
It cannot be a member function, and it needs to return a reference to theostream
so that you can chain<<
as normal.The definition, in your case, would be something like
Note that you return the incoming
ostream
, and print what you like using the<<
operator rather than using the+
operator. It's pretty simple if you follow thisform.
In this case, the data members are public (which is not a good idea in general),
so there's no access problems. If you need to get inaccessible values (because
they're
private
and not exposed in the public interface), you'll need to declarethe operator to be a
friend
in the class definition.operator<<
必须是非成员函数,因为流是左侧参数。在您的情况下,由于数据成员是公共的,因此可以在类外部实现:operator<<
has to be a non-member function, since the stream is the left-hand argument. In your case, since the data members are public, it can be implemented outside the class:谷歌了一下这个,看起来不错:
重载 <<
基本上,当重载时 < ;< IO 运算符,您的函数应如下所示:
注意,该运算符 <<不是类成员,而是外部函数(如果需要,可以是友元)。这样的函数应该使用输出来写入它然后返回它,这样你就可以链接它。
Googled this one, looks fine:
Overloading <<
Basically, when overloading << operator for IO, your function should look like this:
Notice, that operator<< is not a class member, but a external function (which can be friend if you need it to be). Such function should use output to write to it and then return it, so you can chain it.
这里的错误与运算符重载无关,尽管一旦解决,您可能会有更多问题。发生此错误的原因是没有定义接受
const char[5]
和float
参数的operator+
。由于您正在尝试连接这四个参数的字符串形式,因此您必须以编译器可以理解的方式执行此操作,例如
一旦您克服了这一点,您就可以处理您的
<<
重载逻辑。The error here is nothing to do with the operator overloading, though once resolved you may have more questions on that. This error happens because there is no
operator+
defined that takes arguments ofconst char[5]
andfloat
. Since you are trying to concatenate the string forms of those four argsyou have to do this in a way the compiler can understand e.g.
Once you get past this, you can work on your
<<
overloading logic.你可以这样做:
然后你可以这样做:
计算<< your_Line_object <<结束;
You can do this way:
Then you can do:
cout << your_Line_object << endl;
其他人已经解释了正确的方法。我想我应该提到你做错了什么。
您定义一个接受两个 Line 对象的运算符:
当然,这不是您看到的错误。这是由
"y = " + line.m
行引起的。 "y = " 是一个 char[5]。 amd line.m 是一个浮点数,并且没有使用这两个的运算符+(这不是 Basic 或 C#)。问题是 C++ 没有简单的方法将非字符串值“添加”到字符串中。这就是我们使用
cout <<
约定的原因。Other have explained the correct way. I figured I'd mention what you are doing wrong.
You define an operator which takes two Line objects:
Of course, that's not the error you are seeing. That's caused by the line
"y = " + line.m
. "y = " is a char[5]. amd line.m is a float, and there is no operator+ which takes those two (This ain't Basic -- or C#).The problem is that C++ has no easy way to "add" non-string values to a string. Which is why we use the convention of
cout <<
.