C++ 向量实现复制构造函数错误
我正在尝试实现vector的一个版本(用于学习目的)。我的代码无法使用复制构造函数在insert()函数中生成SIGABRT来创建第二个向量。还是找不到我的错误。任何帮助都将不胜感激 数组.hC++ 向量实现复制构造函数错误,c++,arrays,vector,C++,Arrays,Vector,我正在尝试实现vector的一个版本(用于学习目的)。我的代码无法使用复制构造函数在insert()函数中生成SIGABRT来创建第二个向量。还是找不到我的错误。任何帮助都将不胜感激 数组.h #ifndef ARRAY #define ARRAY #include <cstddef> using namespace std; template <typename Object> class Array { private: int s
#ifndef ARRAY
#define ARRAY
#include <cstddef>
using namespace std;
template <typename Object>
class Array
{
private:
int s;
Object *array;
public:
Array()
{
this->array = NULL;
this->s = 0;
}
~Array()
{
this->clear();
}
Array (const Array <Object> &arr)
{
for (int i = 0; i < arr.size(); ++i)
{
this->push_back (arr.array[i]);
}
}
class const_iterator
{
protected:
Object *parent;
public:
const_iterator()
{
}
const_iterator (Object *a)
{
this->parent = a;
}
const_iterator (const const_iterator &itr)
{
this->parent = itr.parent;
}
const Object operator*() const
{
if (this->parent)
{
return *(this->parent);
}
throw 0;
}
const_iterator &operator++()
{
if (this->parent && this->parent + 1)
{
++this->parent;
}
return *this;
}
const_iterator operator++ (int)
{
const_iterator old = *this;
++(*this);
return old;
}
const_iterator &operator--()
{
if (this->parent && this->parent - 1)
{
--this->parent;
}
return *this;
}
const_iterator operator-- (int)
{
const_iterator old = *this;
--(*this);
return old;
}
bool operator== (const const_iterator &rhs) const
{
return this->parent == rhs.parent;
}
bool operator!= (const const_iterator &rhs) const
{
return this->parent != rhs.parent;
}
const_iterator &operator= (const const_iterator &rhs)
{
this->parent = rhs.parent;
return *this;
}
friend class Array <Object>;
};
class iterator : public const_iterator
{
protected:
Object *parent;
public:
iterator()
{
}
iterator (Object *a)
{
this->parent = a;
}
iterator (const iterator &itr)
{
this->parent = itr.parent;
}
const Object &operator*() const
{
if (this->parent)
{
const Object result = *this->parent;
return result;
}
throw 0;
}
Object &operator*()
{
if (this->parent)
{
return *this->parent;
}
throw 0;
}
iterator &operator++()
{
if (this->parent && this->parent + 1)
{
++this->parent;
}
return *this;
}
iterator operator++ (int)
{
const_iterator old = *this;
++(*this);
return old;
}
iterator &operator--()
{
if (this->parent && this->parent - 1)
{
--this->parent;
}
return *this;
}
iterator operator-- (int)
{
const_iterator old = *this;
--(*this);
return old;
}
bool operator== (const iterator &rhs) const
{
return this->parent == rhs.parent;
}
bool operator!= (const iterator &rhs) const
{
return this->parent != rhs.parent;
}
iterator &operator= (const iterator &rhs)
{
this->parent = rhs.parent;
return *this;
}
friend class Array <Object>;
};
iterator begin()
{
return iterator (this->array);
}
const_iterator begin() const
{
return const_iterator (this->array);
}
iterator end()
{
return iterator (this->array + this->s);
}
const_iterator end() const
{
return const_iterator (this->array + this->s);
}
int size() const
{
return this->s;
}
Object &front()
{
if (this->array)
{
return *this->array;
}
throw 1;
}
const Object front() const
{
if (this->array)
{
const Object result = *(this->array);
return result;
}
}
Object &back()
{
if (this->array)
{
return *(this->array + this->s - 1);
}
}
const Object back() const
{
if (this->array)
{
const Object result = *(this->array + this->s - 1);
return result;
}
}
void push_back (Object &o)
{
if (this->s)
this->insert (iterator (this->array + this->s - 1), o);
else
this->insert (iterator (this->array), o);
}
void pop_back()
{
if (this->array)
{
this->erase (iterator (this->array + this->s - 1));
}
}
iterator insert (iterator itr, Object &obj)
{
Object *destination = itr.parent;
if (destination)
{
++this->s;
Object *newArray = new Object [this->s];
int i;
Object *temp = this->array;
for (i = 0; temp != destination; ++i, ++temp)
{
newArray[i] = *temp;
}
newArray[i] = *temp;
++i;
++temp;
newArray[i] = obj;
iterator result (newArray + i);
++i;
while (i < this->s)
{
newArray[i] = *temp;
++i;
++temp;
}
delete [] this->array;
this->array = newArray;
return result;
}
else if (!this->s)
{
++this->s;
this->array = new Object;
*(this->array) = obj;
return iterator (this->array);
}
else
{
return iterator (NULL);
}
}
iterator erase (iterator itr, Object &obj)
{
Object *destination = itr.parent;
if (destination)
{
--this->s;
Object *newArray = new Object [this->s];
int i;
Object *temp = this->array;
for (i = 0; temp != destination; ++i, ++temp)
{
newArray[i] = *temp;
}
++temp;
newArray[i] = *temp;
iterator result (newArray+i);
++i;
++temp;
while (i < this->s)
{
newArray[i] = *temp;
++i;
++temp;
}
return result;
}
else
{
return iterator (NULL);
}
}
void clear()
{
if (this->array)
{
delete [] this->array;
this->s = 0;
}
}
bool operator== (const Array <Object> &rhs)
{
if (this->s != rhs.size())
{
return false;
}
else
{
for (int i = 0; i < this->s; ++i)
{
if (this->array[i] != rhs[i])
{
return false;
}
}
return true;
}
}
bool operator != (const Array <Object> &rhs)
{
if (this->s != rhs.size())
{
return true;
}
else
{
for (int i = 0; i < this->s; ++i)
{
if (this->array[i] != rhs[i])
{
return true;
}
}
return false;
}
}
Array &operator= (const Array <Object> &rhs)
{
if (this->size)
{
delete [] this->array;
this->size = 0;
}
for (int i = 0; i < rhs.size(); ++i)
{
this->push_back (rhs.array[i]);
}
return *this;
}
Object &operator[] (const int index)
{
return this->array[index];
}
};
#endif
#ifndef数组
#定义数组
#包括
使用名称空间std;
模板
类数组
{
私人:
int-s;
对象*数组;
公众:
数组()
{
这个->数组=NULL;
这->s=0;
}
~Array()
{
这个->清除();
}
数组(常量数组和arr)
{
对于(int i=0;i推回(arr.array[i]);
}
}
类常量迭代器
{
受保护的:
对象*父对象;
公众:
常量迭代器()
{
}
常量迭代器(对象*a)
{
此->父项=a;
}
常量迭代器(常量常量迭代器和itr)
{
此->父项=itr.parent;
}
常量对象运算符*()常量
{
如果(此->父级)
{
返回*(此->父级);
}
掷0;
}
常量迭代器和运算符++()
{
如果(本->父项和本->父项+1)
{
++这个->家长;
}
归还*这个;
}
常量迭代器运算符++(int)
{
常量迭代器old=*this;
++(*本条);
返老还童;
}
常量迭代器和运算符--()
{
如果(此->父项&&此->父项-1)
{
--这个->家长;
}
归还*这个;
}
常量迭代器运算符--(int)
{
常量迭代器old=*this;
--(*本条);
返老还童;
}
布尔运算符==(常量迭代器&rhs)常量
{
返回此->父项==rhs.parent;
}
布尔运算符!=(常量迭代器和rhs)常量
{
返回此->父项!=rhs.parent;
}
常量迭代器和运算符=(常量迭代器和rhs)
{
此->父项=rhs.parent;
归还*这个;
}
友元类数组;
};
类迭代器:公共常量迭代器
{
受保护的:
对象*父对象;
公众:
迭代器()
{
}
迭代器(对象*a)
{
此->父项=a;
}
迭代器(常量迭代器和itr)
{
此->父项=itr.parent;
}
常量对象和运算符*()常量
{
如果(此->父级)
{
常量对象结果=*此->父对象;
返回结果;
}
掷0;
}
对象和运算符*()
{
如果(此->父级)
{
返回*此->父项;
}
掷0;
}
迭代器和运算符++()
{
如果(本->父项和本->父项+1)
{
++这个->家长;
}
归还*这个;
}
迭代器运算符++(int)
{
常量迭代器old=*this;
++(*本条);
返老还童;
}
迭代器和运算符--()
{
如果(此->父项&&此->父项-1)
{
--这个->家长;
}
归还*这个;
}
迭代器运算符--(int)
{
常量迭代器old=*this;
--(*本条);
返老还童;
}
布尔运算符==(常量迭代器和rhs)常量
{
返回此->父项==rhs.parent;
}
布尔运算符!=(常量迭代器和rhs)常量
{
返回此->父项!=rhs.parent;
}
迭代器和运算符=(常量迭代器和rhs)
{
此->父项=rhs.parent;
归还*这个;
}
友元类数组;
};
迭代器begin()
{
#include "array.h"
#include <iostream>
int main()
{
Array <int> test;
for (int i = 0; i < 10; ++i)
{
test.push_back (i);
}
for (int i = 0; i < test.size(); ++i)
{
cout << test[i] << " ";
}
cout << endl;
Array <int> test1 (test); /* fails here */
for (int i = 0; i < test1.size(); ++i)
{
cout << test1[i] << " ";
}
cout << endl;
}