E
eric
#ifndef Array_H
#define Array_H
template < class elemType >
class Array {
public:
// parameterize element type
explicit Array( int size = DefaultArraySize );
Array( elemType *array, int array_size );
Array( const Array &rhs );
virtual ~Array() { delete [] ia; }
bool operator==( const Array& ) const;
bool operator!=( const Array& ) const;
Array& operator=( const Array& );
int size() const { return _size; }
virtual elemType& operator[](int index){ return ia[index]; }
virtual void sort();
virtual elemType min() const;
virtual elemType max() const;
virtual int find( const elemType &value ) const;
protected:
static const int DefaultArraySize = 12;
int _size;
elemType *ia;
};
#endif
--------------------------------------------------------------------------------------------------------------
// Array.cpp
#include "Array.h"
/*
Array::Array(elemType *iarray, int iarray_size)
{
ia = iarray;
_size = iarray_size;
}
*/
Array::Array(const Array &rhs )
{
ia = rhs;
}
Array::Array(int size )
{
_size = size;
}
--------------------------------------------------------------------------------
#include <iostream>
#include "Array.h"
int main() {
const int array_size = 4;
// elemType becomes int
Array<int> ia(array_size);
// elemType becomes double
Array<double> da(array_size);
// elemType becomes char
Array<char> ca(array_size);
int ix;
for ( ix = 0; ix < array_size; ++ix ) {
ia[ix] = ix;
da[ix] = ix * 1.75;
ca[ix] = ix + 'a';
}
for ( ix = 0; ix < array_size; ++ix )
std::cout << "[ " << ix << " ] ia: " << ia[ix]
<< "\tca: " << ca[ix]
<< "\tda: " << da[ix] << std::endl;
return 0;
}
------------------------------------------
but my compiler result is
------------
eric@eric-laptop:~/CppPrimer3$ g++ Array.cpp pg52.cpp
Array.cpp:13:1: error: invalid use of template-name ‘Array’ without an
argument list
Array.cpp:18:1: error: invalid use of template-name ‘Array’ without an
argument list
eric@eric-laptop:~/CppPrimer3$
#define Array_H
template < class elemType >
class Array {
public:
// parameterize element type
explicit Array( int size = DefaultArraySize );
Array( elemType *array, int array_size );
Array( const Array &rhs );
virtual ~Array() { delete [] ia; }
bool operator==( const Array& ) const;
bool operator!=( const Array& ) const;
Array& operator=( const Array& );
int size() const { return _size; }
virtual elemType& operator[](int index){ return ia[index]; }
virtual void sort();
virtual elemType min() const;
virtual elemType max() const;
virtual int find( const elemType &value ) const;
protected:
static const int DefaultArraySize = 12;
int _size;
elemType *ia;
};
#endif
--------------------------------------------------------------------------------------------------------------
// Array.cpp
#include "Array.h"
/*
Array::Array(elemType *iarray, int iarray_size)
{
ia = iarray;
_size = iarray_size;
}
*/
Array::Array(const Array &rhs )
{
ia = rhs;
}
Array::Array(int size )
{
_size = size;
}
--------------------------------------------------------------------------------
#include <iostream>
#include "Array.h"
int main() {
const int array_size = 4;
// elemType becomes int
Array<int> ia(array_size);
// elemType becomes double
Array<double> da(array_size);
// elemType becomes char
Array<char> ca(array_size);
int ix;
for ( ix = 0; ix < array_size; ++ix ) {
ia[ix] = ix;
da[ix] = ix * 1.75;
ca[ix] = ix + 'a';
}
for ( ix = 0; ix < array_size; ++ix )
std::cout << "[ " << ix << " ] ia: " << ia[ix]
<< "\tca: " << ca[ix]
<< "\tda: " << da[ix] << std::endl;
return 0;
}
------------------------------------------
but my compiler result is
------------
eric@eric-laptop:~/CppPrimer3$ g++ Array.cpp pg52.cpp
Array.cpp:13:1: error: invalid use of template-name ‘Array’ without an
argument list
Array.cpp:18:1: error: invalid use of template-name ‘Array’ without an
argument list
eric@eric-laptop:~/CppPrimer3$