2

因此,当我尝试创建整数类数组时出现错误。整数 * arr = 新整数 [3]; 这不起作用,我不明白为什么。查看底部的 main() 函数,看看我在哪里有这行代码。

using namespace std;

class Integer
{
    unsigned int * data;
    bool positive;
    int length;
    static const long long BASE = (long long)UINT_MAX + 1;
    void copy(const Integer & from)
    {
        int trim = 0;
        for(int i = 0; i < from.length; i++)
        {
            if(from.data[i] == 0)
                trim++;
            else
                break;
        }
        length = from.length - trim;
        if(length == 0)
        {
            length = 1;
            data = new unsigned int[length];
            data[0] = 0;
        }
        else
        {
            data = new unsigned int[from.length];
            positive = from.positive;
            for(int i = 0, j = trim; i < length; i++, j++)
            {
                data[i] = from.data[j];
            }
        }
    }
    Integer(unsigned int * newData, int len, bool sign)
    {
        data = newData;
        length = len;
        positive = sign;
    }
    static void stripLeadingZeros(unsigned int *& data, int & length)
    {
        int i;
        for(i = 0; i < length; i++)
        {
            if(data[i])
                break;
        }
        if(i == 0)
            return;
        int newLen = length - i;
        unsigned int * tmp = new unsigned int[newLen];
        for(int ni = 0; i < length; i++, ni++)
        {
            tmp[ni] = data[i];
        }
        delete data;
        data = tmp;
        length = newLen;
    }
    public:
    Integer(long long n)
    {
        positive = n >= 0;
        if(!positive)
            n *= -1;
        unsigned int highVal = n >> 32;
        if(highVal == 0)
        {
            length = 1;
            data = new unsigned int[length];
            data[0] = (unsigned int)n;
        }
        else
        {
            length = 2;
            data = new unsigned int[length];
            data[0] = highVal;
            data[1] = (unsigned int)n;
        }
    }
    /*Integer(int n)
    {
        if(n < 0)
        {
            positive = false;
            n *= -1;
        }
        length = 1;
        data = new int[length];
        data[0] = n;
    }*/

    Integer(const Integer & from)
    {
        copy(from);
    }
    Integer & operator=(const Integer& from)
    {
        copy(from);
        return *this;
    }
    ~Integer()
    {
        if(data != NULL)
            delete[] data;
    }
    void add(Integer & n)
    {
        int newLen = max(this->length, n.length) + 1;
        unsigned int * newData = new unsigned int[newLen];
        int carry = 0;
        long long sum;
        for(int i = newLen - 1, thisI = this->length - 1, nI = n.length - 1; i >= 0; i--, thisI--, nI--)
        {
            sum = (thisI >= 0 ? (long long)this->data[thisI] : 0) + (nI >= 0 ? (long long)n.data[nI] : 0) + carry;
            newData[i] = sum % BASE;
            carry = sum / BASE;
        }
        if(newData[0] == 0)
            stripLeadingZeros(newData, newLen);
        delete[] this->data;
        this->length = newLen;
        this->data = newData;
    }
    void add(Integer * numbers, int arrLen)
    {
        int maxLen = 0;
        int * positions = new int[arrLen];
        for(int i = 0; i < arrLen; i++)
        {
            positions[i] = numbers[i].length - 1;
            maxLen = numbers[i].length > maxLen ? numbers[i].length : maxLen;
        }
        int newLen = maxLen + 1;
        unsigned int * newData = new unsigned int[newLen];
        int carry = 0;
        long long sum;
        int thisPos = this->length - 1;
        for(int i = newLen - 1; i >= 0; i--, thisPos--)
        {
            sum = (thisPos >= 0 ? this->data[thisPos] : 0) + carry;
            for(int j = 0; j < arrLen; j++)
            {
                sum += positions[j] >= 0 ? numbers[j].data[positions[j]--] : 0;
            }
            newData[i] = sum % BASE;
            carry = sum / BASE;
        }
        delete[] positions;
        if(newData[0] == 0)
            stripLeadingZeros(newData, newLen);
        delete[] this->data;
        this->length = newLen;
        this->data = newData;
    }
    string to_string()
    {
        stringstream ss;
        for(int i = 0; i < length; i++)
        {
            ss << "(" <<data[i] <<  " x " << BASE << "^" << length - i - 1 << (i != length - 1 ? ") + " : ")");
        }
        return ss.str();
    }
};
int main()
{
    Integer w(234);
    Integer x(243);
    Integer y(234234);
    Integer z(232);
    Integer * a = new Integer[3]; // Error 
    a[0] = w;
    a[1] = x;
    a[2] = y;
    z.add(a, 3);
    cout << x.to_string() << endl;
    delete[] a;
    return 0;
}
4

2 回答 2

5

这会给您错误,因为您的类中没有默认构造函数Integer

在创建 s 数组时Integer,它会尝试调用默认构造函数,Integer而不使用您的类未提供的任何参数。

于 2013-10-23T17:45:51.617 回答
1

您没有默认构造函数。

T 的默认构造函数如下所示:T::T();它想要一个,因为 new 应该构造东西!

于 2013-10-23T17:46:13.340 回答