重载流和算术运算问题

发布于 2024-10-02 23:52:17 字数 1396 浏览 8 评论 0原文

我遇到了重载运算符+和流<<的问题。我有一个带有重载运算符的类:

FuzzyNumber& FuzzyNumber::add(FuzzyNumber B)
{
    FuzzyNumber fuzzyResult;

    fuzzyResult.setA(this -> getA() + B.getA() );
    fuzzyResult.setB(this -> getB() + B.getB() );
    fuzzyResult.setC(this -> getC() + B.getC() );

    return fuzzyResult;
}

FuzzyNumber& operator+(FuzzyNumber& A, FuzzyNumber& B)
{
    return A.add(B);
}

All fields inside class are double type. Here is overloaded ostream operator>>

ostream& FuzzyNumber::streamWrite(ostream& outStream)
{
    outStream << "( "
              << this -> getA() << ", "
              << this -> getB() << ", "
              << this -> getC() << ")";

    return outStream;
}



ostream& operator<< (ostream& outStream, FuzzyNumber& fuzzyNumber)
{
    fuzzyNumber.streamWrite(outStream);
    return outStream;
}

当我输入 cout << 时,打算打印类似 (3, 4.3, 5.1) 的内容。模糊数;。它工作得很好,而且这也工作得很好:

FuzzyNumber fuzzyNumber = numA + numB;
cout << "A + B = " << fuzzyNumber << endl;

其中 numAnumB 是 FuzzyNumber 类型。 虽然当我用以下内容替换上面的行时程序停止运行:

cout << "A + B = " << (numA + numB) << endl;

也许默认运算符=有问题,但是这个类中没有动态变量,所以它不应该。

提前感谢您的帮助!

I have got problem with overloaded operator+ and stream<<. I have class with overloaded operators:

FuzzyNumber& FuzzyNumber::add(FuzzyNumber B)
{
    FuzzyNumber fuzzyResult;

    fuzzyResult.setA(this -> getA() + B.getA() );
    fuzzyResult.setB(this -> getB() + B.getB() );
    fuzzyResult.setC(this -> getC() + B.getC() );

    return fuzzyResult;
}

FuzzyNumber& operator+(FuzzyNumber& A, FuzzyNumber& B)
{
    return A.add(B);
}

All fields inside class are double type. Here is overloaded ostream operator>>

ostream& FuzzyNumber::streamWrite(ostream& outStream)
{
    outStream << "( "
              << this -> getA() << ", "
              << this -> getB() << ", "
              << this -> getC() << ")";

    return outStream;
}



ostream& operator<< (ostream& outStream, FuzzyNumber& fuzzyNumber)
{
    fuzzyNumber.streamWrite(outStream);
    return outStream;
}

It is intend to print something like that (3, 4.3, 5.1) when i type cout << fuzzyNumber;. It works fine, furthermore this also works fine:

FuzzyNumber fuzzyNumber = numA + numB;
cout << "A + B = " << fuzzyNumber << endl;

where numA and numB are FuzzyNumber types.
Although program stops running when I replace above line with this:

cout << "A + B = " << (numA + numB) << endl;

Maybe it is something wrong with default operator=, but there are no dynamic variables in this class, so it shouldn't.

Thanks for help in advance!

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

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

发布评论

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

评论(4

夜灵血窟げ 2024-10-09 23:52:17

在operator+内部,您返回了对堆栈变量的引用。对于这个明显的例子,你的编译器应该警告你。

FuzzyNumber FuzzyNumber::add(FuzzyNumber B) const
{
    FuzzyNumber fuzzyResult;

    fuzzyResult.setA(this -> getA() + B.getA() );
    fuzzyResult.setB(this -> getB() + B.getB() );
    fuzzyResult.setC(this -> getC() + B.getC() );

    return fuzzyResult;
}

FuzzyNumber operator+(const FuzzyNumber& A, const FuzzyNumber& B)
{
    return A.add(B);
}

这段代码应该可以解决你的问题。我还添加了一些适当的 const 正确性。

Inside operator+ you returned a reference to a stack variable. Your compiler should have warned you for this obvious instance.

FuzzyNumber FuzzyNumber::add(FuzzyNumber B) const
{
    FuzzyNumber fuzzyResult;

    fuzzyResult.setA(this -> getA() + B.getA() );
    fuzzyResult.setB(this -> getB() + B.getB() );
    fuzzyResult.setC(this -> getC() + B.getC() );

    return fuzzyResult;
}

FuzzyNumber operator+(const FuzzyNumber& A, const FuzzyNumber& B)
{
    return A.add(B);
}

This code should solve your problem. I also added some proper const correctness.

油焖大侠 2024-10-09 23:52:17

尝试

FuzzyNumber FuzzyNumber::add(FuzzyNumber const &B) const
{
  FuzzyNumber fuzzyResult;

  fuzzyResult.setA(this -> getA() + B.getA() );
  fuzzyResult.setB(this -> getB() + B.getB() );
  fuzzyResult.setC(this -> getC() + B.getC() );

  return fuzzyResult;
}


FuzzyNumber operator+(FuzzyNumber const & A, FuzzyNumber const & B)
{
  FuzzyNumber res(A);
  res.add(B);
  return res;
}

ostream& operator<< (ostream& outStream, FuzzyNumber const & fuzzyNumber)
{
    fuzzyNumber.streamWrite(outStream);
    return outStream;
}

记住,您必须将 FuzzyNumber::streamWrite() 设置为常量!

Try

FuzzyNumber FuzzyNumber::add(FuzzyNumber const &B) const
{
  FuzzyNumber fuzzyResult;

  fuzzyResult.setA(this -> getA() + B.getA() );
  fuzzyResult.setB(this -> getB() + B.getB() );
  fuzzyResult.setC(this -> getC() + B.getC() );

  return fuzzyResult;
}


FuzzyNumber operator+(FuzzyNumber const & A, FuzzyNumber const & B)
{
  FuzzyNumber res(A);
  res.add(B);
  return res;
}

and

ostream& operator<< (ostream& outStream, FuzzyNumber const & fuzzyNumber)
{
    fuzzyNumber.streamWrite(outStream);
    return outStream;
}

Keep in mind that you have to make your FuzzyNumber::streamWrite() const!

玉环 2024-10-09 23:52:17

临时对象不能绑定到非常量引用,只能绑定到常量引用。 operator<< 的重载采用非常量引用,因此它不适用于临时变量。它应该采用 const 引用来代替 FuzzyNumber

ostream& operator<< (ostream& outStream, const FuzzyNumber& fuzzyNumber) {
   ...
}

这还意味着 FuzzyNumberstreamWrite() 和 < code>getA() 到 getC() 也应该声明为常量。

Temporaries can not be bound to non-const references, only to constant ones. The overload of operator<< takes a non-const reference, so it doesn't work for temporaries. It should take the FuzzyNumber by const reference instead:

ostream& operator<< (ostream& outStream, const FuzzyNumber& fuzzyNumber) {
   ...
}

This additionally means that FuzzyNumber's streamWrite() and getA() through getC() should also be declared constant.

晚风撩人 2024-10-09 23:52:17

您的 add 函数返回对将不再存在的本地的引用,从而导致未定义的行为。 add 函数应按值返回以避免这种情况。

Your add function returns a reference to a local that will cease to exist, causing undefined behavior. The add function should return by value to avoid this.

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