超载<< C++ 中的运算符

发布于 2024-09-24 01:11:37 字数 501 浏览 8 评论 0 原文

我想超载<< 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 但出现更多错误。这样做的正确方法是什么?

谢谢 ;)

I want to overload << operator in a Line class so I can print an object using cout like this:

cout << myLineObject << endl;

but this is not working:

class Line{
public:
    float m;
    float b;
    string operator << (Line &line){return ("y = " + line.m + "x + " + line.b);};
};

I get:

Invalid operands of types 'const char [5]' and 'float' to binary 'operator+'

I also tried with stringstream but I get even more errors. What is the correct way of doing this?

Thanks ;)

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

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

发布评论

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

评论(6

风尘浪孓 2024-10-01 01:11:37

讨论重载 << 的地方都列出了正确的方法,而您几乎错过了所有内容。

标准声明是ostream & operator<<(ostream & s, const & Line l); 它不能是成员函数,并且需要返回对 ostream 的引用,以便您可以链接<< 正常。

在您的情况下,定义类似于

ostream & operator<<(ostream & s, const & Line l)
{
    return s << "y = " << l.m << "x + " << l.b;
}

注意您返回传入的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 the ostream so that you can chain << as normal.

The definition, in your case, would be something like

ostream & operator<<(ostream & s, const & Line l)
{
    return s << "y = " << l.m << "x + " << l.b;
}

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 this
form.

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 declare
the operator to be a friend in the class definition.

£冰雨忧蓝° 2024-10-01 01:11:37

operator<< 必须是非成员函数,因为流是左侧参数。在您的情况下,由于数据成员是公共的,因此可以在类外部实现:

std::ostream& operator<<(std::ostream& stream, const Line& line)
{
    return stream << "y = " << line.m << " x = " << line.b;
}

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:

std::ostream& operator<<(std::ostream& stream, const Line& line)
{
    return stream << "y = " << line.m << " x = " << line.b;
}
分开我的手 2024-10-01 01:11:37

谷歌了一下这个,看起来不错:
重载 <<

基本上,当重载时 < ;< IO 运算符,您的函数应如下所示:

friend ostream& operator<<(ostream& output, const YourClassHere& p);

注意,该运算符 <<不是类成员,而是外部函数(如果需要,可以是友元)。这样的函数应该使用输出来写入它然后返回它,这样你就可以链接它。

Googled this one, looks fine:
Overloading <<

Basically, when overloading << operator for IO, your function should look like this:

friend ostream& operator<<(ostream& output, const YourClassHere& p);

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.

冷心人i 2024-10-01 01:11:37

这里的错误与运算符重载无关,尽管一旦解决,您可能会有更多问题。发生此错误的原因是没有定义接受 const char[5]float 参数的 operator+。由于您正在尝试连接这四个参数的字符串形式,因此

"y = " + line.m + "x + " + line.b

您必须以编译器可以理解的方式执行此操作,例如

ostringstream concat;
concat << string("y = ") << line.m << string("x + ") << line.b;
return concat.str();

一旦您克服了这一点,您就可以处理您的 << 重载逻辑。

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 of const char[5] and float. Since you are trying to concatenate the string forms of those four args

"y = " + line.m + "x + " + line.b

you have to do this in a way the compiler can understand e.g.

ostringstream concat;
concat << string("y = ") << line.m << string("x + ") << line.b;
return concat.str();

Once you get past this, you can work on your << overloading logic.

云巢 2024-10-01 01:11:37

你可以这样做:

   class Line{
    public:
    float m;
    float b;
    friend ostream& operator<< (ostream& out, Line& object) {
    out << object.m << endl;
    out << object.b << endl;
    return out;
    }
};

然后你可以这样做:
计算<< your_Line_object <<结束;

You can do this way:

   class Line{
    public:
    float m;
    float b;
    friend ostream& operator<< (ostream& out, Line& object) {
    out << object.m << endl;
    out << object.b << endl;
    return out;
    }
};

Then you can do:
cout << your_Line_object << endl;

撧情箌佬 2024-10-01 01:11:37

其他人已经解释了正确的方法。我想我应该提到你做错了什么。

您定义一个接受两个 Line 对象的运算符:

 Line a;
 Line b;
 string c = a << b;  
 // c would have the string values for line b
 // the values of line a would be ignored.

当然,这不是您看到的错误。这是由 "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:

 Line a;
 Line b;
 string c = a << b;  
 // c would have the string values for line b
 // the values of line a would be ignored.

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 <<.

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