I have an assignment to create a template for a predefined Array
class by using a type parameter and a nontype parameter. The
problem I am having is with my overloaded operators and copy
constructor. My overloaded operators give me an error that
states: "no operator defined which takes a right-hand operand of
type 'class Array<int,10>' (or there is no acceptable
conversion)". My test program gives me the following error when
I attempt to use the copy constructor: "use of class template
requires template argument list."
Could someone tell me where I am going wrong?
Here is my code and the applicable portions of the test program.
<header>
Code:template< class elementType, int numberOfElements = 10 > class Array { .. public: Array(); // default constructor Array( const Array & ); // copy constructor ~Array(); // destructor int getSize() const; // return size // assignment operator const Array &operator=( const Array & ); // equality operator bool operator==( const Array & ) const; // inequality operator; returns opposite of == operator bool operator!=( const Array &right ) const { return ! ( *this == right ); // invokes Array::operator== } // end function operator!= <definition> // copy constructor for class Array; // must receive a reference to prevent infinite recursion template< class elementType, int numberOfElements > Array< elementType, numberOfElements >::Array ( const Array &arrayToCopy ) :size( numberOfElements ) { ptr = new elementType[ size ]; // create space for array for ( int i = 0; i < size; i++ ) ptr[ i ] = arrayToCopy.ptr[ i ]; // copy into object } // end Array copy constructor // overloaded assignment operator; // const return avoids: ( a1 = a2 ) = a3 template< class elementType, int numberOfElements > const Array< elementType, numberOfElements > &Array< elementType, numberOfElements >::operator=( const Array &right ) { if ( &right != this ) { // check for self-assignment // for arrays of different sizes, deallocate original // left-side array, then allocate new left-side array if ( size != right.size ) { delete [] ptr; // reclaim space size = right.size; // resize this object ptr = new elementType[ size ]; // create space for array copy } // end inner if for ( int i = 0; i < size; i++ ) ptr[ i ] = right.ptr[ i ]; // copy array into object } // end outer if return *this; // enables x = y = z, for example } // end function operator= // determine if two arrays are equal and // return true, otherwise return false template< class elementType, int numberOfElements > bool Array< elementType, numberOfElements >::operator==( const Array &right ) const { if ( size != right.size ) return false; // arrays of different sizes for ( int i = 0; i < size; i++ ) if ( ptr[ i ] != right.ptr[ i ] ) return false; // arrays are not equal return true; // arrays are equal } // end function operator== <test program> .. Array< int, 7 > integers1; // seven-element Array Array< int > integers2; // 10-element Array by default .. // use overloaded inequality (!=) operator cout << "\nEvaluating: integers1 != integers2\n"; if ( integers1 != integers2 ) cout << "integers1 and integers2 are not equal\n"; .. // create array integers3 using integers1 as an // initializer; print size and contents Array integers3 ( integers1 ); // calls copy constructor .. // use overloaded assignment (=) operator cout << "\nAssigning integers2 to integers1:\n"; integers1 = integers2; // note target is smaller



LinkBack URL
About LinkBacks


