Page 1 of 1

Template Class Syntax Errors :(

Posted: Sat Jul 21, 2012 11:10 pm
by RandomDever
Here's the class:

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;
}
When I declare a variable with this like so:

Code: Select all

DynamicArray<int> whatever;
It gives me 15 errors including:

Code: Select all

syntax error : missing ';' before '<'
missing type specifier - int assumed. Note: C++ does not support default-int
unexpected token(s) preceding ';'
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 :)

Re: Template Class Syntax Errors :(

Posted: Sun Jul 22, 2012 10:25 am
by Nokurn
The [] is misplaced for the delete operator in the destructor. Array delete has the form of delete[] p, not delete p[]. This is the primary cause of the problem.

The DynamicArray<type>:: before members inside of the methods is telling the compiler that the members are static, which they aren't.

If you don't like having template <class type> everywhere, you can get rid of it by moving all of the method definitions into the class itself, like this:

Code: Select all

template <class type>
class DynamicArray
{
public:
   DynamicArray() { arraySize = 0; }

   ~DynamicArray()
   {
      if( arraySize != 0 )
         delete[] pointer;
   }

   void Initialize( unsigned long int size )
   {
      if( arraySize != 0 )
      {
         type *temp = new type[size];
         for( unsigned long int i = 0; i < size; i++ )
         {
            if( !( arraySize - 1 < i ) )
               temp[i] = DynamicArray<type>::pointer[i];
         }
         delete pointer;
         pointer = temp;
      }
      else
         pointer = new type[size];
      arraySize = size;
   }

   void SetIndex( unsigned long int index, type value )
   {
      if( arraySize != 0 )
      {
         if( index < arraySize )
            pointer[index] = value;
      }
   }

   type GetIndex( unsigned long int index )
   {
      if( arraySize != 0 )
      {
         if( index < arraySize )
            return pointer[index];
      }
   }

   unsigned long int GetSize() { return arraySize; }
private:
   unsigned long int arraySize;
   type *pointer;
};
And as a general suggestion, you should avoid re-implementing containers such as this unless you have an extremely specific reason or if you're doing it for the experience. The STL version will nearly always be faster--unless you were using the wrong container for the situation. Either way, I recommend benchmarking your version and comparing it to the STL analog.

Re: Template Class Syntax Errors :(

Posted: Sun Jul 22, 2012 11:03 am
by RandomDever

Code: Select all

template <class type>
class DynamicArray
{
public:
	DynamicArray()
	{
		DynamicArray<type>::arraySize = 0;
	}
	~DynamicArray()
	{
		if( arraySize != 0 )
		{
			delete[] pointer;
		}
	}
	void Initialize( unsigned long int size )
	{
		if( arraySize != 0 )
		{
			type *temp = new type[size];
			for( unsigned long int i = 0; i < size; i++ )
			{
				if( !( arraySize - 1 < i ) )
				{
					temp[i] = pointer[i];
				}
			}
			delete pointer;
			pointer = temp;
		}
		else
		{
			pointer = new type[size];
		}
		arraySize = size;
	}
	void SetIndex( unsigned long int index, type value )
	{
		if( arraySize != 0 )
		{
			if( index < arraySize )
			{
				pointer[index] = value;
			}
		}
	}
	type GetIndex( unsigned long int index )
	{
		if( arraySize != 0 )
		{
			if( index < arraySize )
			{
				return pointer[index];
			}
		}
	}
	unsigned long int GetSize()
	{
		return arraySize;
	}
private:
	unsigned long int arraySize;
	type *pointer;
};
The updated code still produces the same errors.

My reason for doing this is because I didn't now of any other dynamic array and also for ease of use
because using std::list as I usually do is annoying and SOOOOOOOOOOOOOOO slow.

Re: Template Class Syntax Errors :(

Posted: Sun Jul 22, 2012 11:28 am
by Nokurn
RandomDever wrote:

Code: Select all

DynamicArray<type>::arraySize = 0;
This part is wrong. Get rid of DynamicArray<type>::.
RandomDever wrote:My reason for doing this is because I didn't now of any other dynamic array and also for ease of use because using std::list as I usually do is annoying and SOOOOOOOOOOOOOOO slow.
std::list is a doubly linked list. Therefore, it has fast insertion and deletion anywhere in the list and bidirectional iteration (at the expense of being larger than a linked list), but it doesn't support random access. If the "annoying" behavior you're referring to is the lack of random access, you might want to look at std::vector, which is also faster but doesn't have fast insertion or deletion in the middle of the vector.

Re: Template Class Syntax Errors :(

Posted: Sun Jul 22, 2012 11:45 pm
by RandomDever
But wouldn't std::vector have a bit more RAM overhead than a simple bare-bones class like mine?

Re: Template Class Syntax Errors :(

Posted: Mon Jul 23, 2012 2:34 am
by short
RandomDever wrote:But wouldn't std::vector have a bit more RAM overhead than a simple bare-bones class like mine?
Most likely vector is exactly what you want. If not, there is std::array.

Re: Template Class Syntax Errors :(

Posted: Mon Jul 23, 2012 2:46 am
by RandomDever
Well thanks again Nokurn for the help. I'll use vector from now on.
It's fine and speed is more important to me than RAM anyways.

Re: Template Class Syntax Errors :(

Posted: Mon Jul 23, 2012 12:06 pm
by Rebornxeno
The problem you are having is that you are putting <type> where it shouldn't go. When you define a member function outside of the class, you need to put the class name after the return type and right before the name of the function.

Code: Select all

type class::function(type t)
{
    return t;
}
This is what you are doing

Code: Select all

type class<type>::function(type t)
{
    return t;
}