2

So I am getting an error when I try to create an array of Integer classes. Integer * arr = new Integer[3]; This doesn't work and I don't understand why. Look in my main() function at the bottom to see where I have this line of code.

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 Answers 2

5

This is giving you error because you do not have a default constructor in your Integer class.

When creating an array of Integers, it is trying to call a default constructor for Integer without any arguments that your class doesn't provide.

Sign up to request clarification or add additional context in comments.

Comments

1

You have no default constructor.

The default constructor for T looks like this: T::T(); it wants one, because new is supposed to construct stuff!

2 Comments

Can't it just initialize everything to NULL? Is there a way to specify another constructor to call?
@chasep255 define a constructor that sets the integer to 0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.