i do an exercise and i have 2 questions.
the exercise is to take an array example from my c++ book and make it template.
for start my code is this:
#include <iostream>
#include <iomanip>
#include <cstdlib>
#ifndef ARRAY_H
#define ARRAY_H
using namespace std;
template<typename T,int elements>
class Array
{
// friend ostream &operator<<( ostream &, const Array & );
// friend istream &operator>>( istream &, Array & );
public:
Array();
Array( const Array & );
int getSize() const;
const Array &operator=( const Array & );
bool operator==( const Array & ) const;
bool operator!=( const Array &right ) const
{
return ! ( *this == right );
}
T &operator[]( int );
T operator[]( int ) const;
private:
int size;
T ptr[elements];
};
#endif
template<typename T,int elements>
Array<T,elements>::Array()
{
size = ( elements > 0 ? elements : 10 );
for ( int i = 0; i < size; i++ )
ptr[ i ] = 0;
}
template<typename T,int elements>
Array<T,elements>::Array( const Array &arrayToCopy )
: size( arrayToCopy.size )
{
for ( int i = 0; i < size; i++ )
ptr[ i ] = arrayToCopy.ptr[ i ];
}
template<typename T,int elements>
int Array<T,elements>::getSize() const
{
return size;
}
/*
template<typename T,int elements>
const Array &Array<T,elements>::operator=( const Array &right )
{
if ( &right != this )
{
if ( size != right.size )
{
size = right.size;
ptr = T[ size ];
}
for ( int i = 0; i < size; i++ )
ptr[ i ] = right.ptr[ i ];
}
return *this;
}
*/
template<typename T,int elements>
bool Array<T,elements>::operator==( const Array &right ) const
{
if ( size != right.size )
return false;
for ( int i = 0; i < size; i++ )
if ( ptr[ i ] != right.ptr[ i ] )
return false;
return true;
}
template<typename T,int elements>
T &Array<T,elements>::operator[]( int subscript )
{
if ( subscript < 0 || subscript >= size )
{
cerr << "\nError: Subscript " << subscript
<< " out of range" << endl;
exit( 1 );
}
return ptr[ subscript ];
}
template<typename T,int elements>
T Array<T,elements>::operator[]( int subscript ) const
{
if ( subscript < 0 || subscript >= size )
{
cerr << "\nError: Subscript " << subscript
<< " out of range" << endl;
exit( 1 );
}
return ptr[ subscript ];
}
/*
istream &operator>>( istream &input, Array &a )
{
for ( int i = 0; i < a.size; i++ )
input >> a.ptr[ i ];
return input;
}
ostream &operator<<( ostream &output, const Array &a )
{
int i;
for ( i = 0; i < a.size; i++ )
{
output << setw( 12 ) << a.ptr[ i ];
if ( ( i + 1 ) % 4 == 0 )
output << endl;
}
if ( i % 4 != 0 )
output << endl;
return output;
}
*/
that way works fine but i have put the equality operator in comment and the overloaded ostream and istream as comments cause i recieve errors and i dont know how to fix it.
and 2nd i awnt to make a template specialization of the class with float so i do that
template<>
class Array <float>
{
// friend ostream &operator<<( ostream &, const Array & );
// friend istream &operator>>( istream &, Array & );
public:
Array();
Array( const Array & );
int getSize() const;
const Array &operator=( const Array & );
bool operator==( const Array & ) const;
bool operator!=( const Array &right ) const
{
return ! ( *this == right );
}
float &operator[]( int );
float operator[]( int ) const;
private:
int size;
float ptr[elements];
};
the question is why is wrong?and where i should put the non type parameter
int elements
?