Here is my code:
Class Header file:
#pragma once
class dyn_array
{
int totalSize;
int numberOfElements;
int* data;
public:
dyn_array();
dyn_array(const dyn_array&);
};
Class cpp file:
#include<iostream>
#include "dyn_array.h"
using namespace std;
dyn_array::dyn_array()
{
totalSize = 4;
numberOfElements = 0;
data = new int[totalSize];
}
dyn_array::dyn_array(const dyn_array& copy)
{
totalSize = copy.totalSize;
numberOfElements = copy.numberOfElements;
data = new int[copy.totalSize];
for (int i = 0; i < numberOfElements; i++)
data[i] = copy.data[i]; //this line showing error
}
The compiler is showing me the following error:
Buffer overrun while writing to 'data': the writable size is 'copy.totalSize*4' bytes,
but '8' bytes might be written.
I am new to Object Oriented Programming, please explain me the error, and how can I correct it?