看如下报错的原因是什么?谢谢
代码如下:
头文件:vector.h
#ifndef VECTOR_H#define VECTOR_Htemplate <typename object>class vector{ public: explicit vector(int ini_size = 0); vector(const vector &rhs); ~vector(); const vector & operator= (const vector &rhs); void size() const; void resize(int new_size); void reserve(int new_capacity); object & operator[](int index); const object & operator[](int index) const; void push_back(const object & x); enum{SPARE_CACPCITY = 16}; private: int the_size; int the_capacity; object *objects;};#endif
#include "vector.h"vector::vector(int init_size):the_size(init_size),the_capacity(init_size + SPARE_CAPACITY){ the_size = init_size; objects = new object[the_capacity];}vector::vector(vector & rhs):objects(NULL){ operator=(rhs);}~vector::vector(){ delete [] objects;}const vector::vector & operator=(const vector & rhs){ if(this != &rhs){ delete [] objects; the_size = rhs.size(); the_capactity = rhs.the_capacity; objects = object[the_capacity]; for (int i = 0; i < the_size; i++){ objects[i] = rhs[i]; } return *this; }}void size() const{ return the_size;}void resize(int new_size){ if(new_size > the_capacity){ the_size = new_size * 2 + 1; reserve(the_size); } the_size = new_size;}void reserve(int new_capacity){ if(new_capacity < the_capacity){ return; } object * old_objects = objects; objects = new object[new_capacity]; for(int i=0; i < old_objects.size(); i++){ objects[i] = old_objects[i]; } the_capacity = new_capacity; delete [] old_objects;}object & operator[](int index){ return objects[index];}void push_back(object & x){ if(the_size == the_capacity){ reserve(2 * the_capacity + 1); } objects[the_size++] = x;}
#include "vector.h"#include <iostream>#include <string>using namespace std;int main(){ string str("abc"); vector<string> vc; vc.push_back(str); cout << vc[0] << endl; return 0;}