3,881
社区成员
发帖
与我相关
我的任务
分享
In file included from test.cpp:1:0:
EDynamicArray.h:56:14: error: ‘T’ was not declared in this scope
void CDArray<T>::Init()
^
EDynamicArray.h:56:15: error: template argument 1 is invalid
void CDArray<T>::Init()
^
EDynamicArray.h: In function ‘void Init()’:
EDynamicArray.h:58:2: error: ‘m_nSize’ was not declared in this scope
m_nSize = 0;
^~~~~~~
EDynamicArray.h:59:2: error: ‘m_nMax’ was not declared in this scope
m_nMax = 1;
^~~~~~
EDynamicArray.h:60:2: error: ‘m_pData’ was not declared in this scope
m_pData = NULL;
^~~~~~~
EDynamicArray.h: At global scope:
EDynamicArray.h:76:14: error: ‘T’ was not declared in this scope
bool CDArray<T>::InvalidateIndex(int nIndex)
^
EDynamicArray.h:76:15: error: template argument 1 is invalid
bool CDArray<T>::InvalidateIndex(int nIndex)
^
EDynamicArray.h: In function ‘bool InvalidateIndex(int)’:
EDynamicArray.h:78:29: error: ‘m_nSize’ was not declared in this scope
if( nIndex < 0 || nIndex > m_nSize)
^~~~~~~
EDynamicArray.h: At global scope:
EDynamicArray.h:319:13: error: ‘template<class T> class CDArray’ used without template parameters
CDArray<T>& CDArray::operator=(const CDArray& array)
^~~~~~~
EDynamicArray.h:319:38: error: invalid use of template-name ‘CDArray’ without an argument list
CDArray<T>& CDArray::operator=(const CDArray& array)
^~~~~~~
In file included from test.cpp:1:0:
EDynamicArray.h:15:7: note: ‘template<class T> class CDArray’ declared here
class CDArray
^~~~~~~
#ifndef CDARRAY_H
#define CDARRAY_H
#include<iostream>
using namespace std;
template <typename T>
class CDArray
{
private:
T *m_pData; //store pointer of the array
int m_nSize; //numbers of the element of the array
int m_nMax; //predistributed memory size for dynamic array
private:
void Init(); //initiation
void Free(); //free the dynamic memory
inline bool InvalidateIndex(int nIndex); //validate the suffix
public:
CDArray(); //default constructor
CDArray(int nSize, T dValue = 0); //other constructors
CDArray(const CDArray& arr); //copy constructor
~CDArray(); //deconstructor
void Print() const; //print all elements of the array
int GetSize() const; //get size of the array
int SetSize(int nSize); //reset the size of the array.if nSize>size of current array, we set first nSize elelments as the elements of the array.If not, we set augmented elements as zero.
T GetAt(int nIndex); //get value of number nIndex
T operator[] (int nIndex) const; //reloading operator[] to get value of the array by a[k].
int SetAt(int nIndex, T dValue); //set value of number nIndex
int PushBack(T dValue); //add a new element to the end
int DeleteAt(int nIndex); //delete one element
int InsertAt(int nIndex, T dValue); //insert a new element at nIndex.
CDArray& operator=(const CDArray& array); //reload operator of assignment.
/*#define SAFEDELETE(p) if(p) {delete p;p=NULL;}*/
/*#define SAFEDELETE(p)if(p) {delete[]p;p=NULL;}*/
#endif
};
/*Initiation*/
void CDArray<T>::Init()
{
m_nSize = 0;
m_nMax = 1;
m_pData = NULL;
}
/*release the unused memory*/
template <class T>
void CDArray<T>::Free()
{
//if( m_pData != NULL )
//{
delete [] m_pData;
// m_pData = NULL;
//}
}
/*validate nIndex*/
bool CDArray<T>::InvalidateIndex(int nIndex)
{
if( nIndex < 0 || nIndex > m_nSize)
{
return false;
}
else
return true;
}
//am not sure with this part,but general idea is correct.
/*default constructor*/
template <class T>
CDArray<T>::CDArray() //default constructor
{
Init();
}
/*constructor*/
template <class T>
CDArray<T>::CDArray(int nSize, T dValue)
{
if(nSize > m_nMax)
{
m_nMax = 2 * m_nMax;
m_nSize = nSize;
m_pData = new T[m_nMax]; //augmented
for(int i=0; i<m_nSize; ++i)
m_pData[i] = dValue;
}
else
{
if(nSize = 0)
{
Init();
}
else
{
m_nSize = nSize;
for(int i = 0; i<m_nSize; ++i)
m_pData[i] = dValue;
}
}
}
/*copy constructor*/
template <class T>
CDArray<T>::CDArray(const CDArray<T>& arr)
{
m_nSize = arr.m_nSize;
m_pData = new T[m_nSize];
//*m_pData = *(arr.m_pData);
memcpy(m_pData, arr.m_pData, m_nSize*sizeof(T));//augmented
}
/*deconstructor*/
template <class T>
CDArray<T>::~CDArray()
{
Free();
}
/*print the array*/
template <class T>
void CDArray<T>::Print() const
{
if( m_nSize == 0)
cout << "Error:the empty array can't be printed!" << endl;
//if(m_pData == NULL)
//{
// cout << "Error:the empty array can't be printed.";
// exit(0);
//}
else
{
for(int i=0; i<m_nSize; ++i)
cout << m_pData[i] << " ";
// printf("%lf \n", m_pData[i]);
cout << endl;
}
}
/*get the size of the array*/
template <class T>
int CDArray<T>::GetSize() const
{
return m_nSize;
}
/*set the size of the array*/
template <class T>
int CDArray<T>::SetSize(int nSize)
{
if(nSize <= m_nSize)
{
for(int i = nSize; i<m_nSize; ++i)
{
m_pData[i] = 0;
}
}
if (nSize > m_nMax)
{
m_nMax = 2*m_nMax;
T *temp = new T[m_nMax];
memcpy(temp, m_pData, m_nSize * sizeof(T));
for(int i=m_nSize; i<nSize; ++i)//错误写法:i=nSize!
{
temp[i] = 0;
}
delete [] m_pData;
m_pData = temp;
}
else
{
for(int i=m_nSize; i<nSize; ++i)
m_pData[i] = 0;
}
m_nSize = nSize; //Brilliant codes!
return 1;
}
/*get the value of number nIndex*/
template <class T>
T CDArray<T>::GetAt(int nIndex)
{
if(InvalidateIndex(nIndex))
return m_pData[nIndex];
else
{
cout << "Error:the index in GetAt is invalid!";
exit(0);
}
}
/*reload operator[]*/
template <class T>
T CDArray<T>::operator[] (int nIndex) const
{
if(nIndex < 0 || nIndex >= m_nSize)
{
cout << "Error:the index in [] is invalid!"<<endl;
exit(0);
}
return m_pData[nIndex];
}
/*set the value of number nIndex as dValue*/
template <class T>
int CDArray<T>::SetAt(int nIndex, T dValue)
{
if(!InvalidateIndex(nIndex))
{
cout << "Error;the index of SetAt is invalid!" << endl;
exit(0);
}
else
{
m_pData[nIndex] = dValue;
}
return 1;
}
/*add an element at the end of the array*/
template <class T>
int CDArray<T>::PushBack(T dValue)
{
int tempSize = m_nSize +1;
if(tempSize < m_nMax)
m_pData[m_nSize] = dValue;
else
{
m_nMax = 2*m_nMax;
T* temp = new T[m_nMax]; //size problem?
memcpy(temp, m_pData, m_nSize*sizeof(T));
delete [] m_pData;
m_pData = temp;
m_pData[m_nSize] = dValue;
}
++m_nSize;
return 1;
}
/*delete the element of number nIndex*/
template <class T>
int CDArray<T>::DeleteAt(int nIndex)
{
if(InvalidateIndex(nIndex))
{
for(int i = nIndex; i < m_nSize; ++i)//结束条件:i= ?
m_pData[i] = m_pData[i+1];
m_pData[m_nSize-1] = 0;
--m_nSize;
}
else
{
cout << "Error:the index of DeleteAt is invalid!";
exit(0);
}
return 1;
}
/*Insert one element at nIndex*/
template <class T>
int CDArray<T>::InsertAt(int nIndex, T dValue)
{
if(InvalidateIndex(nIndex))
{
int tempSize = m_nSize+1;
if(tempSize < m_nMax)
{
for(int i=m_nSize-1; i>=nIndex; --i)
m_pData[i+1] = m_pData[i];
}
else
{
m_nMax = 2*m_nMax;
T *temp = new T[m_nMax];
memcpy(temp, m_pData, m_nSize*sizeof(T));
delete [] m_pData;
m_pData = temp;
for(int i=m_nSize-1; i>=nIndex; --i)
m_pData[i+1] = m_pData[i];
}
m_pData[nIndex] = dValue;
}
else
{
cout << "Error: the index of InsertAt is invalid!" << endl;
exit(0);
}
++m_nSize;
return 1;
}
/*reload operator,= */
template <class T>
CDArray<T>& CDArray::operator=(const CDArray& array)
{
if(this == &array)
return *this;
m_nSize = array.m_nSize;
T *m_temp = new T[m_nSize];
memcpy(m_temp, array.m_pData, m_nSize*sizeof(T));
delete [] m_pData;
m_pData = m_temp;
return *this;
}