我试图建立一个矢量风格类,并以使用模板还有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;
}
Run Code Online (Sandbox Code Playgroud)
编译器(Dev C++)在错误号1上写入此错误:
无效使用未定义类型`struct type2'
错误号码2上的此错误:
`newone'的类型不完整
但是,我不明白什么是错的.任何提示?
您需要指定用于这些方法的模板或将函数实现移动到类中.
template <class type2>
storage<class type2>:: ~storage()
{
delete[] organs;
}
Run Code Online (Sandbox Code Playgroud)
可能是最简单的解决方案.同样适用于您的第二个错误.
编辑:
找到了一个很好的模板教程,其中包括这个.
http://www.cplusplus.com/doc/tutorial/templates/