重载流和算术运算问题
我遇到了重载运算符+和流<<的问题。我有一个带有重载运算符的类:
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;
其中 numA
和 numB
是 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在operator+内部,您返回了对堆栈变量的引用。对于这个明显的例子,你的编译器应该警告你。
这段代码应该可以解决你的问题。我还添加了一些适当的 const 正确性。
Inside operator+ you returned a reference to a stack variable. Your compiler should have warned you for this obvious instance.
This code should solve your problem. I also added some proper const correctness.
尝试
并
记住,您必须将 FuzzyNumber::streamWrite() 设置为常量!
Try
and
Keep in mind that you have to make your FuzzyNumber::streamWrite() const!
临时对象不能绑定到非常量引用,只能绑定到常量引用。
operator<<
的重载采用非常量引用,因此它不适用于临时变量。它应该采用 const 引用来代替FuzzyNumber
:这还意味着
FuzzyNumber
的streamWrite()
和 < 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 theFuzzyNumber
by const reference instead:This additionally means that
FuzzyNumber
'sstreamWrite()
andgetA()
throughgetC()
should also be declared constant.您的
add
函数返回对将不再存在的本地的引用,从而导致未定义的行为。add
函数应按值返回以避免这种情况。Your
add
function returns a reference to a local that will cease to exist, causing undefined behavior. Theadd
function should return by value to avoid this.