0
我試圖創建將在數組的末尾插入值的模板函數,但我一直運行到賽格故障無論什麼我嘗試做。任何建議將不勝感激。模板化插入在端功能
template <typename T>
void ArrayList<T>::insert_back(const T& x)
{
if(m_size == m_max)
{
m_max = m_max*2; //resize array
T* P = new T[m_max];
for(int y = 0; y < m_size; y++) //copying array
{
P[y] = m_data[y];
}
delete[] m_data; //copy done delete
m_data = P;
P[m_size] = x; //gdb segfault -- caused by accessing P[m_size]
m_size ++;
}
else // no resizing
{
m_data[m_size] = x;
m_size++;
}
}
什麼是'm_size','m_max'和'm_data'初始化爲? – JaredC 2013-02-13 05:31:31