我有一个任务,我要负责创建一个程序来构造双向链表以容纳非常长的整数。创建用于operator +重载的方法以添加两个非常长的int对象的任务之一。当我尝试使用operator <<方法来打印单个对象时,它的效果很好,并且可以打印出我期望的结果。但是,当我添加两个对象时,什么也没打印出来……好像我的对象丢失了。谁能帮我调试一下吗?为什么在调用operator +方法之后,我的LongInt对象没有被打印到控制台?
问题是a + b返回空白: https://laracasts.com/discuss/channels/laravel/running-python-in-laravel
// Arithmetic Binary Operator +
// Adds two LongInt objects
// Input: a LongInt object
// Output: a new LongInt which is the sum of lhs (current) and rhs (arguement)
LongInt LongInt::operator+( const LongInt &rhs ){
Deque<char> lhs = digits;
Deque<char> rhs2 = rhs.digits;
Deque<char> newLongInt;
int carry = 0;
while (!lhs.isEmpty() && !rhs2.isEmpty()){
newLongInt.addFront((lhs.getBack()+rhs2.getBack() + carry) % 10);
carry = (lhs.removeBack() + rhs2.removeBack()) / 10;
}
LongInt test;
test.digits = newLongInt;
return test;
}
// Prints LongInt to console
// Input: Reference to LongInt
// Output: string of LongInt printed out
ostream &operator<<( ostream &out, const LongInt &rhs ) {
// check if LongInt is negative and if so, add '-'
rhs.negative == true ? out << "-" : out << "";
// temp deque<char>
Deque<char> temp = rhs.digits;
// check if it is empty
if (temp.isEmpty()) {
out << "0";
} else {
// loop through linked list then output LongInt
while(!temp.isEmpty()){
out << temp.removeFront();
}
}
return out;
}