Template Class Syntax Errors :(

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
RandomDever
Chaos Rift Regular
Chaos Rift Regular
Posts: 198
Joined: Thu Mar 26, 2009 8:42 pm
Current Project: My Engine
Programming Language of Choice: C++

Template Class Syntax Errors :(

Post 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 :)
User avatar
Nokurn
Chaos Rift Regular
Chaos Rift Regular
Posts: 164
Joined: Mon Jan 31, 2011 12:08 pm
Favorite Gaming Platforms: PC, SNES, Dreamcast, PS2, N64
Programming Language of Choice: Proper C++
Location: Southern California
Contact:

Re: Template Class Syntax Errors :(

Post 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.
RandomDever
Chaos Rift Regular
Chaos Rift Regular
Posts: 198
Joined: Thu Mar 26, 2009 8:42 pm
Current Project: My Engine
Programming Language of Choice: C++

Re: Template Class Syntax Errors :(

Post 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.
User avatar
Nokurn
Chaos Rift Regular
Chaos Rift Regular
Posts: 164
Joined: Mon Jan 31, 2011 12:08 pm
Favorite Gaming Platforms: PC, SNES, Dreamcast, PS2, N64
Programming Language of Choice: Proper C++
Location: Southern California
Contact:

Re: Template Class Syntax Errors :(

Post 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.
RandomDever
Chaos Rift Regular
Chaos Rift Regular
Posts: 198
Joined: Thu Mar 26, 2009 8:42 pm
Current Project: My Engine
Programming Language of Choice: C++

Re: Template Class Syntax Errors :(

Post by RandomDever »

But wouldn't std::vector have a bit more RAM overhead than a simple bare-bones class like mine?
User avatar
short
ES Beta Backer
ES Beta Backer
Posts: 548
Joined: Thu Apr 30, 2009 2:22 am
Current Project: c++, c
Favorite Gaming Platforms: SNES, PS2, SNES, SNES, PC NES
Programming Language of Choice: c, c++
Location: Oregon, US

Re: Template Class Syntax Errors :(

Post 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.
My github repository contains the project I am currently working on,
link: https://github.com/bjadamson
RandomDever
Chaos Rift Regular
Chaos Rift Regular
Posts: 198
Joined: Thu Mar 26, 2009 8:42 pm
Current Project: My Engine
Programming Language of Choice: C++

Re: Template Class Syntax Errors :(

Post 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.
Rebornxeno
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 85
Joined: Thu Jun 23, 2011 11:12 am

Re: Template Class Syntax Errors :(

Post 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;
}
Post Reply