++ operator?

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;
}



help pleaseee, thank you!!
Um, I believe the ++ operator needs to (or at least should) return a reference to the just-incremented object you called it with.

Furthermore, what exactly "doesn't work"? We'll need to know that to help you any further. :/

-Albatross
It's not working because it's just adding one to a temporary.
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++();"

thanks!
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).
@ hanst99
1
2
3
4
5
void bignum:: operator++()
{
    bignum one(1);
    (*this) = (*this) + one;
}


the above code also gives an error; thread stops at "bignum one(1);"

and this
1
2
3
4
void bignum:: operator++()
{
    (*this) = (*this) + bignum one (1);
}


fails to build :(

and my + operator for the bignum class works fine
Last edited on
Three things.

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;
...?

-Albatross
Last edited on
I'm using the default constructor
1
2
3
bignum::bignum()
{
}


It's a small project so if I could just use the + operator for bignums, it'd be good.
Thanks!
...um... the constructor we were more interested in seeing is one that looks something like this:

1
2
3
bignum::bignum(int something) {
//Code here.
}


That's the one you're calling, no? :)

-Albatross
oh right, do you mean this?
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
// 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);
}
Your constructor doesn't actually do anything (well, nothing of what it's supposed to do anyways).
changed the constructor to
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// 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
Topic archived. No new replies allowed.