I am doing a project for school and it requires a copy constructor, destructor, etc. When I use the copy constructor an error is thrown saying the array is empty, I assume the copy constructor is not working.
When I delete the copy constructor the program works, meaning the issue is probably happening within that function.
template<class T>
class DynArray {
public:
DynArray<T>(){
ptr = new T[Capacity];
this->Capacity = 2;
this->Size = 0;
}
DynArray<T>(T n) {
ptr = new T[Capacity];
this->Capacity = n;
this->Size = 0;
}
DynArray<T>(const DynArray& orig) {
cout << "Copy" << endl;
ptr = new T[Size];
*ptr = *(orig.ptr);
}
DynArray<T>& operator=(const DynArray<T>& orig) {
if(this != &orig) {
delete[] ptr;
ptr = new T[Size];
*ptr = *(orig.ptr);
}
return *this;
}
void push_back(const T& n) {
if (Size >= Capacity) {
adjust(Capacity * 2);
}
ptr[Size] = n;
Size++;
}
void adjust(T a) {
cout << "grow" << endl;
T* arr = new T[a];
for (int i = 0; i < Capacity; ++i) {
arr[i] = ptr[i];
}
Capacity = a;
ptr = arr;
}
T& back() {
if(Size == 0) {
throw runtime_error("Array is empty");
}
return ptr[Size - 1];
}
T& front() {
if(Size == 0) {
throw runtime_error("Array is empty");
}
return ptr[0];
}
private:
T* ptr = nullptr;
int Capacity;
int Size;
main:
#include <iostream>
#include "dynarray.h"
using namespace std;
int main( )
{
const char START = 'A';
const int MAX = 12;
// create a vector of chars
DynArray<char> vectD;
// push some values into the vector
for (int i = 0; i < MAX; i++)
{
vectD.push_back(START + i);
}
// remove the last element
vectD.pop_back();
// add another value
vectD.push_back('Z');
// test memory management
DynArray<char> vectD2 = vectD;
// display the contents
cout << "\n[";
for (int i = 0; i < vectD2.size() - 1; i++)
{
cout << vectD2.at(i) << ", ";
}
cout << "..., " << vectD2.back() << "]\n";
DynArray<char> vectD3;
vectD3 = vectD2;
cout << "\n[";
for (int i = 0; i < vectD3.size() - 1; i++)
{
cout << vectD3.at(i) << ", ";
}
cout << "..., " << vectD3.back() << "]\n";
vectD3.front() = '{';
vectD3.back() = '}';
cout << vectD3.front();
for (int i = 1; i < vectD3.size() - 2; i++)
{
cout << vectD3.at(i) << ", ";
}
cout << vectD3.at(vectD3.size()-2) << vectD3.back() << endl;
}
Later in my code I have it set to throw a runtime_error if Size == 0, it does throw the error meaning that the array is empty. Is the copy constructor copying correctly? Main cannot be changed, it is a given from the prof.
UPDATE: I changed the copy constructor to copy all of the elements of the array, but the runtime_error is still returning saying the array is empty.
template<class T>
class DynArray {
public:
DynArray<T>(){
ptr = new T[Capacity];
this->Capacity = 2;
this->Size = 0;
}
DynArray<T>(T n) {
ptr = new T[Capacity];
this->Capacity = n;
this->Size = 0;
}
DynArray<T>(const DynArray& orig) {
cout << "Copy" << endl;
ptr = new T[Size];
for (int i = 0; i < Size; i++) {
ptr[i] = orig.ptr[i];
}
}
DynArray<T>& operator=(const DynArray<T>& orig) {
if(this != &orig) {
delete[] ptr;
ptr = new T[Size];
for (int i = 0; i < Size; i++) {
ptr[i] = orig.ptr[i];
}
}
return *this;
}
void push_back(const T& n) {
if (Size >= Capacity) {
adjust(Capacity * 2);
}
ptr[Size] = n;
Size++;
}
void adjust(T a) {
cout << "grow" << endl;
T* arr = new T[a];
for (int i = 0; i < Capacity; ++i) {
arr[i] = ptr[i];
}
Capacity = a;
ptr = arr;
}
T& back() {
if(Size == 0) {
throw runtime_error("Array is empty");
}
return ptr[Size - 1];
}
T& front() {
if(Size == 0) {
throw runtime_error("Array is empty");
}
return ptr[0];
}
private:
T* ptr = nullptr;
int Capacity;
int Size;
*ptris just the first element of the array, not the actual array. C arrays do not have value semantics and can't be copied by assignment anyway.std::vectorbefore reinventing the wheel.Size? It appears you are using its value in the copy constructor and copy assignment operator without copying the value from the source object.DynArrayin your question, as it is essential to giving an accurate answer.