2014-11-14 20 views
-4

我正在處理一個可以處理大於4個uint32_t元素的大整數的代碼。我創建了一個名爲BigInteger的類和一些運算符。問題是我得到一些錯誤,但我沒有看到發生了什麼問題。我已經將這些錯誤添加爲註釋。希望可以有人幫幫我。C++ - 自制大整數類給出了錯誤

在此先感謝!

/*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; 
} 
+0

也,你的代碼將是簡單了很多,如果你使用了'uint32_t的[4]',而不是4個獨立的'uint32_t's 。 – 2014-11-14 19:09:52

+0

您是否正在編譯至少C++ 0x標準? – 2014-11-14 19:11:07

+0

上面的答案解決了我的問題,但現在我已經有了一些新的答案。我正在編譯2011版標準版。 – qweabc 2014-11-14 19:27:31

回答

0

你沒有說你得到什麼錯誤,但你添加了一些在線評論。最好從代碼中分別發佈確切的錯誤消息(並指出它來自哪條線,以及您期望該線路要做什麼)。

望着這一個:

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

出現了問題。 a.LeftLeft == b.leftLeft的結果是truefalse。然後你就完成了=0這個任務,所以你正在嘗試true = 0false = 0,這兩者都不是有效的賦值。您不能將0分配給右值。

我不太確定你的代碼試圖做什麼。如果你的意思是檢查,如果這兩個變量都爲零,則代碼:

if (a.leftLeft == 0 && b.leftLeft == 0) 
相關問題