I have a template in a 1-file .h file, and I need it to be adjusted and modified to work properly with only a partial amount of the items in it used. The testArray.cpp file I include is how I test if it works properly. As it is now, it displays garbage data for the first 5-6 runs through, then it properly displays numbers. I just need it to work 100%. A lot of the problem is within the += operator I feel, I don't know how to properly amend it.

Here's the .h:

// File: Array.h
// Template array class definition and implementation
// Written by Daniel Spiegel

#ifndef ARRAY_H
#define ARRAY_H

#include <iostream>
#include <cassert>

using namespace std;

template <class eltType> class Array {
    public:

        // default constructor
        Array(int n = 10);

        // constructor initializes elements to given value
        Array(int n, const eltType &val);

        // constructor initializes from a standard array
        Array(const eltType A[], int n);

        // copy constructor
        Array(const Array<eltType> &A);

        // destructor
        ~Array();

        // append element to array
        Array<eltType> &operator+=(eltType);

        // inspector for size of the list
        int size() const { return capacity; }

        // assignment operator
        Array<eltType> &operator=(const Array<eltType> &A);

        // inspector for element of a constant list
        const eltType& operator[](int i) const;

        // inspector for element of a nonconstant list
        eltType& operator[](int i);

        // returns the amount of items currently in use
        int itemsUsed() const { return itemAmt; }

        //increments the amount of items by 1
        int incItemAmt() const { return itemAmt++; }
    private:

        // data members
        eltType *elements;// pointer to list elements

        int capacity; // size of list

        int itemAmt; // amount of items in use in the array
};

// default constructor
template <class eltType> Array<eltType>::Array(int n) 
{
    assert(n > 0);
    capacity = n;
    elements = new eltType [n];
    assert(elements);
}

// constructor initializes elements to given value
template <class eltType>
Array<eltType>::Array(int n, const eltType &val) 
{
    assert(n > 0);
    capacity = n;
    elements = new eltType [n];
    assert(elements);
    for (int i = 0; i < n; ++i) {
        elements[i] = val;
    }
}

// constructor initializes from a standard array
template <class eltType>
Array<eltType>::Array(const eltType A[], int n) 
{
    assert(n > 0);
    capacity = n;
    elements = new eltType [n];
    assert(elements);
    for (int i = 0; i < n; ++i) {
        elements[i] = A[i];
    }
}

// copy constructor
template <class eltType> Array<eltType>::Array(const Array<eltType> &A) 
{
    itemAmt = A.size();
    elements = new eltType [A.size()];
    assert(elements);
    for (int i = 0; i < itemAmt; ++i)
        elements[i] = A[i];
}

// destructor
template <class eltType> Array<eltType>::~Array() 
{
    delete [] elements;
}

// Append element to array
template <class eltType> Array<eltType> &Array<eltType>::operator+=(eltType elt, const Array<eltType> &A)
{
    if (A.itemsUsed() != A.size())
    {
        elements[A.itemsUsed() + 1] = elt;
        A.incItemAmt();
    }
    else if (A.itemsUsed() == A.size())
    {
        eltType *temp=new eltType[capacity+1];

        for (int i = 0; i < size(); ++i)
            temp[i]=elements[i];

        temp[capacity++]=elt;

        delete [] elements;

        elements=temp;

        return(*this);
    }
}

// assignment
template <class eltType>
Array<eltType>& Array<eltType>::operator=(const Array<eltType> &A) 
{
    if (this != &A) 
    {
        if (size() != A.size()) 
        {
            delete [] elements;
            capacity = A.size();
            elements = new eltType [A.size()];
            assert(elements);
        }

        for (int i = 0; i < A.size(); ++i)
            elements[i] = A[i];
    }
    return *this;
}

// inspector of the value of an individual element
template <class eltType>
const eltType& Array<eltType>::operator[](int i) const 
{
    assert((i >= 0) && (i < size()));

    return elements[i];
}

// inspector/mutator facilitator of individual element
template <class eltType>
eltType& Array<eltType>::operator[](int i) 
{
    assert((i >= 0) && (i < size()));

    return elements[i];
}

// template insertion operator for Array
template <class eltType>
ostream& operator<<(ostream &sout, const Array<eltType> &A)
{
    sout << "[ ";

    for (int i = 0; i < A.itemsUsed(); ++i)
        sout << A[i] << " ";

    sout << "]";

    return sout;
}

// insertion operator for Array<char>
ostream& operator<<(ostream &sout,const Array<char> &A) 
{
    for (int i = 0; i < A.itemsUsed(); ++i)
        sout << A[i];

    return sout;
}

#endif

and the testArray.cpp:

#include <iostream>
#include "Array.h"

using namespace std;

int main()
{  
    Array<int> iList(5);

   for (int i=0; i<5; i++) 
   {
     iList += i + 2 * 3;
     cout << iList << " has capacity " << iList.size() << " and " << 
                            iList.itemsUsed() << " elements " <<endl;
   }  

   for (int i=0; i<5; i++) 
   {
     iList += i + 2 * 8;  
     cout << iList << " has capacity " << iList.size() << " and " << 
                            iList.itemsUsed() << " elements " <<endl;
   }  

   cout << iList << endl;

   Array<double> dList(3, 3.75);

   cout << dList << endl;

   char letters[]={'A','C','L','g','8'};

   Array<char> cList(letters, 4);

   cout << cList << endl;

   dList += 9.32;

   cout << dList << endl;
}   

Thanks!

Recommended Answers

All 4 Replies

Line 113 of your header file does not match its declaration. You have an extraneous array parameter.

Yes I know that. That was me experimenting. It does not work either way.

// Append element to array
template <class eltType> Array<eltType> &Array<eltType>::operator+=(eltType elt, const Array<eltType> &A)
{
    if (A.itemsUsed() != A.size())
    {
        elements[A.itemsUsed() + 1] = elt;
        A.incItemAmt();
    }
    else if (A.itemsUsed() == A.size())
    {
        eltType *temp=new eltType[capacity+1];

        for (int i = 0; i < size(); ++i)
            temp[i]=elements[i];

        temp[capacity++]=elt;

        delete [] elements;

        elements=temp;

        return(*this);
    }
}

Oh man this is a pretty confusing operator. Consider just renaming it .push_back or something. Also your problem is that itemAmt isn't being initialzed inside your constructor.

ALright, I inititalized it in the constructor, thanks for pointing that out. Still have no idea about the rest of the code though

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.