【问题标题】:C++ - Self made Big integer class gives errorsC++ - 自制大整数类给出错误
【发布时间】:2014-11-14 19:06:30
【问题描述】:

我正在编写一段代码,它可以处理由 4 个 uint32_t 元素构建的大整数。我创建了一个名为 BigInteger 的类和一些运算符。问题是我得到了一些错误,但我不知道出了什么问题。我已将行后面的错误添加为 cmets。希望可以有人帮帮我。

提前致谢!

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

【问题讨论】:

  • 另外,如果您使用 uint32_t[4] 而不是 4 个单独的 uint32_ts,您的代码会简单得多。
  • 您是否至少使用 C++0x 标准进行编译?
  • 上面的答案解决了我的问题,但现在我有了一些新的问题。我正在用 2011 标准版编译。

标签: c++ compiler-errors biginteger uint32-t


【解决方案1】:

您没有确切说明您遇到了什么错误,但您添加了一些内联 cmets。最好将确切的错误消息与代码分开发布(并指出它来自哪一行,以及您希望该行做什么)。

看看这个:

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

有问题。 a.LeftLeft == b.leftLeft 的结果是 truefalse。然后你在上面做=0 的赋值,所以你正在尝试true = 0false = 0,它们都不是有效的赋值。您不能将 0 分配给右值。

我不太确定您的代码试图做什么。如果您要检查这两个变量是否都为零,那么代码是:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多