Ok - so i'm attempting to create a template class, for creating arrays. Sort of a bit like what vector does, but specific to my needs. More than anything, it's an exercise in creating templates. However, i cannot get the following to compile, and i can't see where i've gone wrong.........
This is a bit more advanced than anything i've ever done before, so any help on this is greatly appreciated.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
|
template <typename T>
class FixedArray {
public:
FixedArray(int initSize = 0);
FixedArray(FixedArray<T>& other);
~FixedArray(void);
int Find(T& value);
T* Start() { return mBuffer; }
int GetLength() { return mLength; }
BOOL SetLength(int newLength);
T& operator [](int index) {return mBuffer[index]};
FixedArray<T>& operator =(FixedArray<T>& other);
protected:
T* mBuffer;
int mLength;
};
template <typename T>
FixedArray<T>::FixedArray(int initSize)
{
mBuffer = NULL;
mLength = 0;
SetLength(initSize);
}
template <typename T>
FixedArray<T>::FixedArray(FixedArray<T>& other)
{
mBuffer = NULL;
mLength = 0;
*this = other;
}
template <typename T>
FixedArray<T>::~FixedArray(void)
{
SetLength(0);
}
template <typename T>
int FixedArray<T>::Find(T& value)
{
int rv=-1;
for (int lp=0; rv<0 && lp<mLength; lp++)
{
if (mBuffer[lp]==value)
rv=lp;
}
return rv;
}
template <typename T>
BOOL FixedArray<T>::SetLength(int newLength) //using BOOL rather than bool
{
if (newLength != mLength)
{
if (newLength == 0)
{
free(mBuffer);
mBuffer=NULL;
}
else
{
if (mLength == 0)
{
mBuffer = (T*) malloc(newLength * sizeof(T));
}
else
{
mBuffer = (T*) realloc(mBuffer, newLength * sizeof(T));
}
}
mLength=newLength;
}
return TRUE;
}
template <typename T>
FixedArray<T>& FixedArray<T>::operator =(FixedArray<T>& other)
{
SetLength(other.mLength);
memcpy(mBuffer,other.mBuffer, mLength * sizeof(T));
return *this;
}
class point2d{
public:
point2d() {};// default constructor
~point2d(); // destructor
float distance(point2d p);
float mX,mY; // member variables
};
typedef FixedArray<point2d> point2darray;
typedef FixedArray<int> intfixedarray;
typedef FixedArray<float> floatfixedarray;
point2d::~point2d(void){
// nothing in here, yet
}
float point2d::distance(point2d p){
float dist=0;
float x=0, y=0;
x=(mX-p.mX);
y=(mY-p.mY);
dist=sqrt((x*x)+(y*y)); // calculates the distance between 2 points
return dist;
}
int main(void)
{
return 0;
}
|
--------------------------------------------------------------------------
with some of the errors being:
1>c:\program files (x86)\microsoft visual studio 9.0\vc\include\codeanalysis\sourceannotations.h(47) : error C2011: 'vc_attributes::YesNoMaybe' : 'enum' type redefinition
1> c:\program files\microsoft visual studio 9.0\vc\include\codeanalysis\sourceannotations.h(47) : see declaration of 'vc_attributes::YesNoMaybe'
1>c:\program files (x86)\microsoft visual studio 9.0\vc\include\codeanalysis\sourceannotations.h(57) : error C2011: 'vc_attributes::AccessType' : 'enum' type redefinition
1> c:\program files\microsoft visual studio 9.0\vc\include\codeanalysis\sourceannotations.h(57) : see declaration of 'vc_attributes::AccessType'
1>c:\program files (x86)\microsoft visual studio 9.0\vc\include\codeanalysis\sourceannotations.h(71) : error C2011: 'vc_attributes::Pre' : 'struct' type redefinition
1> c:\program files\microsoft visual studio 9.0\vc\include\codeanalysis\sourceannotations.h(71) : see declaration of 'vc_attributes::Pre'