Re: invalid use of template-name 'Array' without an argument(compile
error, plz help)
On 6/10/2011 5:15 PM, eric wrote:
#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 )
Should probably be
template<clas T> Array<T>::Array(const Array<T>& rhs)
{
ia = rhs;
FAQ section 10. Prefer initialization over assignment.
}
Array::Array(int size )
Same here. You're defining a constructor of a class template, and you
need the 'template' declaration:
template<class T> Array<T>::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$
--------------------
Is that Array is reserve word in C++ so we can not use it?
No. 'Array' is a template-id. In order to use it you need the template
argument list (like you did in 'main' when declaring an object of a
concrete type).
so idea what these errors talk about, plz help, thank a lot in
advance, Eric
Also, please read the FAQ section 35 (if memory serves). You're likely
to encounter linker errors since the definitions of the member functions
of your 'Array' template are not available to the compiler at the time
when it needs it (while compiling the 'main' function).
V
--
I do not respond to top-posted replies, please don't ask