[英]overloaded streams and arithmetic operations problem
I have got problem with overloaded operator+ and stream<<. 我遇到了重载operator +和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;. 当我键入cout << FuzzyNumber;时,打算打印类似的内容(3,4.3,5.1)。 It works fine, furthermore this also works fine:
它可以正常工作,此外,它也可以正常工作:
FuzzyNumber fuzzyNumber = numA + numB;
cout << "A + B = " << fuzzyNumber << endl;
where numA
and numB
are FuzzyNumber types. 其中
numA
和numB
是FuzzyNumber类型。 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. 也许默认的operator =有点问题,但是此类中没有动态变量,因此应该没有。
Thanks for help in advance! 预先感谢您的帮助!
Inside operator+ you returned a reference to a stack variable. 在operator +内部,您返回了对堆栈变量的引用。 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.
我还添加了一些适当的const正确性。
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! 请记住,必须使FuzzyNumber :: streamWrite()常量!
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. operator<<
的重载采用非常量引用,因此不适用于临时对象。 It should take the FuzzyNumber
by const reference instead: 它应该通过const引用获取
FuzzyNumber
:
ostream& operator<< (ostream& outStream, const FuzzyNumber& fuzzyNumber) {
...
}
This additionally means that FuzzyNumber
's streamWrite()
and getA()
through getC()
should also be declared constant. 这还意味着,
FuzzyNumber
的streamWrite()
和getA()
到getC()
也应声明为常量。
Your add
function returns a reference to a local that will cease to exist, causing undefined behavior. 您的
add
函数返回对本地将不再存在的引用,从而导致未定义的行为。 The add
function should return by value to avoid this. add
函数应按值返回以避免这种情况。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.