E
E
Egor Nameles2018-10-19 12:56:16
C++ / C#
Egor Nameles, 2018-10-19 12:56:16

How to fix a bug in a class template?

Good afternoon. I need to write a program using a template. That is, a dynamic array class template. Here is my code. I'm trying to test it. Everything is entered fine, but I can't figure out the output. For example, I enter the length of the array = 4. And I enter numbers from 1 to 4. At the output I get the following: -842150451 1 2 -842150451 3 -842150451 -842150451 4. Tell me, please, what is the cause of this error and how can I fix it?
Array.h

#pragma once
#include <iostream>
#include <conio.h>
using namespace std;

template <typename T>
class Array
{
  T* arr;
  int size;
  int begin;
  int end;
  void create(int n, int len, int beg);
public:
  Array();
  Array(int n);
  int length();
  Array& clear();
  Array& init(int n);
  Array& pushFront(T a);
  Array& pushBack(T a);
  T popFront();
  T popBack();
  T& operator[](int i);
  ~Array();
};

template <typename T> Array<T>::Array()
{
  create(1, 0, 0);
  //size = 1;
  //arr = new T;
}

template <typename T> Array<T>::Array(int n)
{
  create(n, n, 0);
  /*if (n < 1)
  {
    n = 1;
  }
  size = n;
  arr = new T[n];*/
}

template <typename T> Array<T>::~Array()
{
  delete[] arr;
}

template <typename T> void Array<T>::create(int n, int len, int beg)
{
  if (n < 1)
  {
    n = 1;
  }
  if (len < 0)
  {
    len = 0;
  }
  size = n;
  begin = beg;
  end = beg + len;
  arr = new T[n];
}

template <typename T> int Array<T>::length()
{
  return end - begin;
}

template <typename T> Array<T>& Array<T>::clear()
{
  delete[] arr;
  create(1, 0, 0);
  return *this;
}

template <typename T> Array<T>& Array<T>::init(int n)
{
  delete[] arr;
  create(n, n, 0);
  return *this;
}

template <typename T> Array<T>& Array<T>::pushFront(T a)
{
  if (begin == 0)
  {
    T* temp = arr;
    create(size + length() / 2 + 1, length(), begin + length() / 2 + 1);
    for (int i = 0; i < length(); i++)
    {
      arr[begin + i] = temp[i];
    }
    delete[] temp;
  }
  --begin;
  arr[begin] = a;
  return *this;
}

template <typename T> Array<T>& Array<T>::pushBack(T a)
{
  if (end = size)
  {
    T* temp = arr;
    create(size + length() / 2 + 1, length(), begin);
    for (int i = begin; i < end; i++)
    {
      arr[i] = temp[i];
    }
    delete[] temp;
  }
  arr[end] = a;
  ++end;
  return *this;
}

template <typename T> T Array<T>::popFront()
{
  if (begin > length())
  {
    T* temp = arr;
    int temp2 = begin;
    create(size - begin, length(), 0);
    for (int i = 0; i < length(); ++i)
    {
      arr[i] = temp[temp2 + 1];
    }
    delete[] temp;
  }
  if (length() > 0)
  {
    ++begin;
  }
  return arr[begin - 1];
}

template <typename T> T Array<T>::popBack()
{
  if (size - end > length())
  {
    T* temp = arr;
    create(end, length(), begin);
    for (int i = begin; i < end; i++)
    {
      arr[i] = temp[i];
    }
    delete[] temp;
  }
  if (length() > 0)
  {
    --end;
  }
  return arr[end];
}

template <typename T> T& Array<T>::operator[](int i)
{
  if (i < 0)
  {
    i = 0;
  }
  else if (i >= length())
  {
    i = length() - 1;
  }
  return arr[begin + i];
}

main.cpp
#include <iostream>
#include <conio.h>
#include "Array.h"
using namespace std;

int main()
{
  setlocale(LC_ALL, "Russian");

  int N;
  cout << "Введите размер массива: ";
  cin >> N;
  Array<int>A;
  for (int i = 0; i < N; i++)
  {
    int t;
    cin >> t;
    A.pushBack(t);
  }
  for (int i = 0; i < A.length(); i++)
  {
    cout << A[i] << " ";
  }

  _getch();
  return 0;
}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question