i'm trying implement version of vector (for learning purposes). code fails create second vector copy constructor raising sigabrt in insert() function. still can't find mistake. appreciated.
array.h
#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) { (int = 0; < 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; (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; (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 { (int = 0; < 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 { (int = 0; < 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; } (int = 0; < rhs.size(); ++i) { this->push_back (rhs.array[i]); } return *this; } object &operator[] (const int index) { return this->array[index]; } }; #endif
test.cpp
#include "array.h" #include <iostream> int main() { array <int> test; (int = 0; < 10; ++i) { test.push_back (i); } (int = 0; < test.size(); ++i) { cout << test[i] << " "; } cout << endl; array <int> test1 (test); /* fails here */ (int = 0; < test1.size(); ++i) { cout << test1[i] << " "; } cout << endl; }
you don't initialize array
, s
in copy constructor.
Comments
Post a Comment