2013-05-06 94 views
-2

我試圖實現矢量版本(用於學習目的)。我的代碼無法在insert()函數中創建第二個帶有複製構造函數的SIGABRT向量。仍然無法找到我的錯誤。任何幫助將不勝感激。矢量實現複製構造函數錯誤

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) 
      { 
       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 

TEST.CPP

#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; 
} 
+0

您在構造函數中設置了'this-> array = NULL;',然後您只需在拷貝構造函數中解引用'nullptr'。這導致UB導致seg故障。 'this'需要指向一些有效的內存位置。 – 2013-05-06 07:15:32

+0

發佈時可以去掉不必要的空白區域。 – ChiefTwoPencils 2013-05-06 08:05:17

+0

如果您確實需要爲我們調試代碼,那麼至少不要使用數百萬換行符來發送垃圾郵件。 – 2013-05-06 08:05:48

回答

4

你不拷貝構造函數初始化arrays