[英]Compile errors with templates and new/delete operators (C++)
我試圖建立一個矢量風格的類,並且為了使用模板以及new
, delete
運算符,我有以下這段代碼:
template <class type2> class storage
{
private:
type2 *organs;
public:
int num;
storage(); //constructor
~storage(); //destructor
void operator+(type2 newone);
void operator-(int howmany);
type2 operator[](int place);
};
storage<class type2>:: ~storage()
{
delete[] organs; //~~~~~~~Error number 1~~~~~~~~~~
}
void storage<class type2>:: operator+(type2 newone)
{ // ~~~~~~~~~~~Error number 2~~~~~~~~~~~~~~
organs = new type2[1];
num++;
oragns[num-1] = newone;
}
編譯器(Dev C ++)在錯誤1上寫入此錯誤:
無效使用未定義類型`struct type2'
而此錯誤出現在錯誤編號2:
`newone'具有不完整的類型
但是,我不明白怎么了。 有什么提示嗎?
您需要指定用於這些方法的模板,或將函數實現移至類中。
template <class type2>
storage<class type2>:: ~storage()
{
delete[] organs;
}
可能是最簡單的解決方案。 同樣的方法也可以解決第二個錯誤。
編輯:
找到了一個不錯的模板教程,其中涵蓋了其他內容。
除了編譯錯誤外,此代碼中還有一些主要錯誤。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.