创建一个类的数组

时间:2013-10-23 17:44:46

标签: c++ arrays

因此,当我尝试创建一个Integer类数组时,我收到错误。整数* arr = new整数[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;
}

2 个答案:

答案 0 :(得分:5)

由于您的Integer课程中没有默认构造函数,因此您会收到错误。

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

答案 1 :(得分:1)

您没有默认构造函数。

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