Hi, I'm writing a class called bignum, type vector of char.
I'm trying to write the ++ operator but it's not working... The ++ is a prefix (as in (“++i,” where i is a bignum)).
In private I have :
1 2
private:
vector<char>v;
and this is my ++ operator:
1 2 3 4 5 6
void bignum:: operator++()
{
bignum one(1);
bignum u = (*this);
u = u + one;
}
When I run it on Xcode the program gives an error saying the thread stops at "bignum one(1)."
Is there no way I could do it without return value?
It's a homework project and the given function prototype is "void operator++();"
You don't need to return a value (although you should), but what the ++ operator needs to do is modify the internal state is the object. You aren't doing that. Oh, and when your thread stops at bignum one(1); then your bignum constructor is faulty (or you're calling it wrong).
First, have you heard about the += operator? It basically saves you the trouble of having to write this: (*this) = (*this) + one;
...by compacting it into this: (*this) += one;
Second, I agree with hanst. Would you mind showing us that particular constructor?
Third... wouldn't it be a good idea to define your basic operators operator for normal integral types as well so that you could just do this: (*this) += 1;
...?
// this function flips the order of a vector num
// and returns the result as the vector backwards
vector<char>flip(vector<char>num)
{
vector<char>backwards(num.size());
for (size_t i=0; i<num.size(); i++)
backwards[i] = num[num.size() - i - 1];
return backwards;
}
// converts integer into a vector of type character
bignum::bignum(int number)
{
vector<char>reverse;
int base = 10;
do {
reverse.push_back(number%10 + '0');
number = number / base;
} while (number);
vector<char>converted;
converted = flip(reverse);
}
// converts integer into a vector of type character
bignum::bignum(int number)
{
int base = 10;
if (number == 0)
v.push_back('0');
else
{
do {
v.push_back(number%10 + '0');
number = number / base;
} while (number/10 != 0);
}
v = flip(v);
}
can someone pls tell me what's wrong with it? thankss