Template Class Syntax Errors :(
Posted: Sat Jul 21, 2012 11:10 pm
Here's the class:
When I declare a variable with this like so:
It gives me 15 errors including:
And all three of these errors are repeated 5 times.
This is the first time I've had to use templates and I spent hours yesterday
on Google and tried multiple solutions but none worked (including putting 'DynamicArray<type>::' before every member).
All help is greatly appreciated
Code: Select all
template <class type>
class DynamicArray
{
public:
DynamicArray();
~DynamicArray();
void Initialize( unsigned long int size );
void SetIndex( unsigned long int index, type value );
type GetIndex( unsigned long int index );
unsigned long int GetSize();
private:
unsigned long int arraySize;
type *pointer;
};
template <class type>
DynamicArray<type>::DynamicArray()
{
DynamicArray<type>::arraySize = 0;
}
template <class type>
DynamicArray<type>::~DynamicArray()
{
if( DynamicArray<type>::arraySize != 0 )
{
delete DynamicArray<type>::[]pointer;
}
}
template <class type>
void DynamicArray<type>::Initialize( unsigned long int size )
{
if( DynamicArray<type>::arraySize != 0 )
{
type *temp = new type[size];
for( unsigned long int i = 0; i < size; i++ )
{
if( !( DynamicArray<type>::arraySize - 1 < i ) )
{
temp[i] = DynamicArray<type>::pointer[i];
}
}
delete DynamicArray<type>::pointer;
DynamicArray<type>::pointer = temp;
}
else
{
DynamicArray<type>::pointer = new type[size];
}
DynamicArray<type>::arraySize = size;
}
template <class type>
void DynamicArray<type>::SetIndex( unsigned long int index, type value )
{
if( DynamicArray<type>::arraySize != 0 )
{
if( index < DynamicArray<type>::arraySize )
{
DynamicArray<type>::pointer[index] = value;
}
}
}
template <class type>
type DynamicArray<type>::GetIndex( unsigned long int index )
{
if( DynamicArray<type>::arraySize != 0 )
{
if( index < DynamicArray<type>::arraySize )
{
return DynamicArray<type>::pointer[index];
}
}
}
template <class type>
unsigned long int DynamicArray<type>::GetSize()
{
return DynamicArray<type>::arraySize;
}
Code: Select all
DynamicArray<int> whatever;
Code: Select all
syntax error : missing ';' before '<'
missing type specifier - int assumed. Note: C++ does not support default-int
unexpected token(s) preceding ';'
This is the first time I've had to use templates and I spent hours yesterday
on Google and tried multiple solutions but none worked (including putting 'DynamicArray<type>::' before every member).
All help is greatly appreciated