C++ - Self made Big integer class gives errors

253 views Asked by At

I'm working on a piece of code that can can handle big integers build out of 4 uint32_t elements. I've created a class called BigInteger, and a few operators. The problem is that I get some errors, but I do not see what is going wrong. I've added the errors behind the lines as comments. Hope someone can help me.

Thanks in advance!

/*The big integer exist of 4 uint32_t elements, the integer is equal to leftLeftleftrightrightRight*/

#include <iostream>
#include <stdint.h>

class BigInteger { 
public:

BigInteger() {

    sign = 0;
    leftLeft = 0;
    left = 0;
    right = 0;
    rightRight = 0;
}

BigInteger(bool inputSign, uint32_t inputLeftLeft, uint32_t inputLeft, uint32_t inputRight, uint32_t inputRightRight){   //ERROR MESSAGE: unknown type name 'uint32_t' 

    sign = inputSign;
    leftLeft = inputLeftLeft;
    left = inputLeft;
    right = inputRight;
    rightRight = inputRightRight;
}

uint32_t leftLeft, left, right, rightRight;  
bool sign;  
}; 

/*This part checks if the two integers are equal*/

bool operator==(BigInteger a, BigInteger b) {
bool c;
if (a.sign == b.sign & a.left == b.left & a.leftLeft == b.leftLeft & a.right == b.right & a.rightRight == b.rightRight){
    c = 1;
} 
else {
    c = 0;
}
return 0;
}

/*This part checks if the integer a is bigger then integer b*/

bool operator>(BigInteger a, BigInteger b) {
bool c;
if (a.leftLeft > b.leftLeft) {
    c = 1;
} 
else if (a.leftLeft == b.leftLeft = 0){   //ERROR MESSSAGE: expression is not assignable
    if (a.left > b.left) {
        c = 1;
    } 
    else if (a.left == b.left = 0){    //ERROR MESSSAGE: expression is not assignable
        if (a.right > b.right) {
            c = 1;
        } 
        else if(a.right == b.right = 0){    //ERROR MESSSAGE: expression is not assignable
            if (a.rightRight > b.rightRight) {
                c = 1;
            } 
            else {
                c = 0;
            }
        }
        else {
            c = 0;
        }
    } 
    else {
        c = 0;
    }
}
else {
    c = 0;
}
return c;
}

/*This part makes the integer negative*/

BigInteger operator -(BigInteger a ){    //ERROR MESSAGE: Non-Aggregate type 'BigInteger' can not be initialized with an initializer list.
bool temp;
if(a.sign==0){
    temp = 1;
}
else{
    temp = 0;
}    
BigInteger c = {temp, a.leftLeft, a.left, a.right, a.rightRight};
return c;
}
1

There are 1 answers

0
M.M On BEST ANSWER

You didn't say exactly what errors you were getting, but you added some in-line comments. It's best to post the exact error message separately from the code (and also indicate which line it came from, and what you expected that line to do).

Looking at this one:

if (a.leftLeft == b.leftLeft = 0){

there is a problem. The result of a.LeftLeft == b.leftLeft is either true or false. Then you do the assignment =0 on that, so you are attempting either true = 0 or false = 0, neither of which are valid assignments. You can't assign 0 to an rvalue.

I'm not really sure what your code was trying to do. If you meant to check if both of those variables are zero then the code is:

if ( a.leftLeft == 0 && b.leftLeft == 0 )