我們剛剛在我的comp sci類中引入了泛型和模板。 我被要求創建一個支持任何數據類型的存儲和檢索的通用容器類。 我有我的所有功能工作正常,除非調整我的數組。我在我的插入函數中調用了大小調整:C++使用泛型/模板調整數組大小
template< typename T >
void Container<T>::insert(T magic)
{
if (index == size)
{
resize();
}
containerPtr[index] = magic;
index++;
}
大小變量是數組大小,索引是下一個插入位置。
,這裏是我的調整大小功能:
template< typename T >
void Container<T>::resize()
{
int doubSize = size * 2;
Container<T> temp(doubSize);
for (int i = 0; i < size; i++)
{
temp[i] = containerPtr[i]; // error 1 here
}
*containerPtr = temp; // error 2 here
size = doubSize;
}
我重載=
template< typename T >
const T Container<T>::operator=(const T& rhs)
{
if(this == &rhs)
{
return *this;
}
return *rhs;
}
我收到以下錯誤,當我嘗試編譯:
1: error C2676: binary '[': 'Container<T>' does not define this operator or a conversion to a type acceptable to the predefined operator
2: error C2679: binary '=': no operator found which takes a right-hand operand of type 'Container<T>' (or there is no acceptable conversion)
我不知道我在這裏錯了...
首先=是assigment運算符,你不分配任何東西。 2.在退貨聲明中解除這一點。 3.重新分配存儲.... 4.刪除第二個for循環等 – neagoegab
'containerPtr'是什麼? – ecatmur
http://courses.cms.caltech.edu/cs11/material/cpp/donnie/cpp-ops.html – neagoegab