小编Tal*_*l S的帖子

使用模板和新/删除运算符编译错误(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;
}
Run Code Online (Sandbox Code Playgroud)

编译器(Dev C++)在错误号1上写入此错误:

无效使用未定义类型`struct type2'

错误号码2上的此错误:

`newone'的类型不完整

但是,我不明白什么是错的.任何提示?

c++ templates

0
推荐指数
1
解决办法
358
查看次数

矢量调整大小是如何实现的?

我试图找出一个向量在已经"创建"一些之后如何添加更多对象,我的意思是:

int *ptr;
ptr = new int;
Run Code Online (Sandbox Code Playgroud)

如何使用它,你可以为该指针添加更多的对象吗?(把它变成一个数组)
谢谢!

c++ pointers add new-operator

0
推荐指数
1
解决办法
2797
查看次数

标签 统计

c++ ×2

add ×1

new-operator ×1

pointers ×1

templates ×1