I've been trying to make a dynamic array template class recently and i've run into some problems on witch i have no idea how to fix so here is the code...
#ifndef DYNAMICARRAY_H
#define DYNAMICARRAY_H
template<typename c_type>
class dynamicArray
{
c_type * theArray;
int m_max;
public:
//The Constructor
dynamicArray(int arraySize) : m_max(arraySize), theArray(NULL)
{
theArray = new c_type [arraySize];
if(theArray==0)
{
return;
}
for(int a=0; a<arraySize; a++)
{
theArray[a]=0;
}
m_max=arraySize;
}
//Another constructor
dynamicArray() : m_max(0), theArray(NULL)
{
//ya....
}
//The Destructor
~dynamicArray()
{
delete [] theArray;
}
//Function to delete the Dynamic array
void erase()
{
delete [] theArray;
theArray=NULL;
m_max=0;
}
//set the size of the dynamic array before it's been allocated
void allocate(int size, bool changeMax = true)
{
theArray = new c_type [size];
if(theArray==0)
{
return;
}
for(int a=0; a<size; a++)
{
theArray[a]=0;
}
if(changeMax)
m_max=size;
}
//This will wipe the dynamic array and reszie it, basicly reseting it
void reallocate(int size)
{
erase();
allocate(size);
}
//this will change the size of the currently allocated dynamic array
void resize(int size)
{
c_type * backup = NULL;
backup = new c_type [max];
if(backup==0)
{
return;
}
for(int a=0; a<m_max; a++)
{
backup[a]=theArray[a];
}
erase();
allocate(size, false);
for(int a=0; a<m_max; a++)
{
theArray[a]=backup[a];
}
for(int a=m_max; a<size; a++)
{
theArray[a]=0;
}
delete [] backup;
m_max=size;
}
//get the length of the array
int getLength()
{
return m_max;
}
//overload the [] operator so you can access the values as such
c_type& operator[](int value)
{
if(value<0 || value>m_max)
return theArray[0];
return theArray[value];
}
//copy another dynamic array into this one
void copy(dynamicArray<c_type> &reference)
{
erase();
allocate(reference.getLength());
for(int a=0; a<m_max; a++)
theArray[a]=reference[a];
}
};
#endif
while the errors i'm getting are as follows:
1 2 3 4 5 6 7 8 9 10 11 12
1> c:\users\cory powell\documents\visual studio 2010\projects\random map design\random map design\dynamicarray.h(66): error C2440: 'initializing' : cannot convert from 'overloaded-function' to 'unsigned int'
1> Context does not allow for disambiguation of overloaded function
1> c:\users\cory powell\documents\visual studio 2010\projects\random map design\random map design\dynamicarray.h(64) : while compiling classtemplate member function 'void dynamicArray<c_type>::resize(int)'
1> with
1> [
1> c_type=keyItem
1> ]
1> c:\users\cory powell\documents\visual studio 2010\projects\random map design\random map design\map.h(11) : see reference to classtemplate instantiation 'dynamicArray<c_type>' being compiled
1> with
1> [
1> c_type=keyItem
1> ]
Should line 66 read "backup = new c_type [size];"?
no but you saying that somehow pointed me out to my stupid mistake of saying max instead of m_max...but now i have lots of linker errors...time to get working....