I am working on an operator+ method so that I can add two lists of chars together and overall create a class that can manage "integers" of any size. It works beautifully (as in x + y actually works) when x and y have the same number of items in the list, just not when they are different in size. I added this piece of code to ensure that I wouldn't have the smaller value's iterator trying to dereference the end of the list:
1 2 3 4
|
if (xCurr == tempX.val.end() )
tempX.val.push_back('0');
if (yCurr == tempY.val.end() )
tempY.val.push_back('0');
|
and it only gets that far if at least one of the two (x and y) still has digits left. When I debug and input "11" for x and "111" for y and try to add them I actually get two results. One is that the debugger just crashes and says:
"list iterator not dereferencable"
The other result is that the function will run most of the way and then crash after seeming to miss the piece of code I mentioned above, as if it didn't exist but just had to try to execute this statement:
tempSumInt = (*xCurr - '0') + (*yCurr - '0') + (*cCurr - '0');
while xCurr (my iterator for my tempXobject) was at the end of the list.
Here is the function call I used:
cout << "x + y = " << x + y << ".\n";
and here is the full code so far for the function itself:
(Note: "val" is the identifier of the list<char> member in the private section of the LongInt class).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
|
LongInt operator+(const LongInt& x, const LongInt& y) //friend function
{
LongInt tempX = x, tempY = y, carry, sum;
list<char>::const_iterator xCurr, yCurr, cCurr;
int tempSumInt;
char tempSumChar;
tempX.val.reverse();
tempY.val.reverse();
xCurr = tempX.val.begin();
yCurr = tempY.val.begin();
cCurr = carry.val.begin();
while (! (xCurr == tempX.val.end() && yCurr == tempY.val.end() ) )
{
if (xCurr == tempX.val.end() )
tempX.val.push_back('0');
if (yCurr == tempY.val.end() )
tempY.val.push_back('0');
tempSumInt = (*xCurr - '0') + (*yCurr - '0') + (*cCurr - '0');
if (tempSumInt > 9)
{
tempSumInt = tempSumInt % 10;
tempSumChar = static_cast<char>(tempSumInt + '0');
sum.val.push_back(tempSumChar);
carry.val.push_back('1');
xCurr++;
yCurr++;
cCurr++;
}
else
{
tempSumChar = static_cast<char>(tempSumInt + '0');
sum.val.push_back(tempSumChar);
carry.val.push_back('0');
xCurr++;
yCurr++;
cCurr++;
}
}
sum.val.pop_front();
sum.val.reverse();
return sum;
}
|
Thank you very much for your time in reading this! If you have any questions please ask. Hopefully the identifiers are clear enough :)
Ok so I figured out that, if it reached the end of the list and I wanted to add the extra digit to the end of the list to let it continue until both had finished, I also had to decrement the iterator (since the iterator would remain pointing at the end of the list even while I had put another value in front). I assumed the iterator would point to the '0' automatically but this fixed it:
1 2 3 4 5 6 7 8 9 10
|
if (xCurr == tempX.val.end() )
{
tempX.val.push_back('0');
--xCurr;
}
if (yCurr == tempY.val.end() )
{
tempY.val.push_back('0');
--yCurr;
}
|