I get that it's related to my default declaration, but I don't know what I've done wrong.
Basically, if I declare the Array class with the input length argument it works just fine.
If I use the default declaration with no argument, and then define it afterward, it throws the error at end of scope when it tries to delete "y".
Can anyone tell me what I'm doing wrong here/how to fix it? If it's too complex to answer, can you direct me to the proper place to learn about it?
#include <iostream>
using namespace std;
class Array {
private:
int length;
double* values;
public:
Array();
Array(int);
~Array();
double* getValuesPointer() { return values; };
};
Array::Array() {
length = 0;
values = 0;
};
Array::Array(int inputLength) {
length = inputLength;
values = new double[length];
for (int n = 0; n < length; n++) {
values[n] = 0;
};
};
Array::~Array() {
cout << "Attempting to delete." << endl;
delete[] values;
cout << "Successfully deleted." << endl;
};
int main() {
// This works:
/*
Array x(10);
double* vp0 = x.getValuesPointer();
cout << vp0[0] << "\n" << endl;
*/
// This runs but crashes at end of scope when it tries to delete "y":
Array y;
y = Array(10);
double* vp1 = y.getValuesPointer();
cout << vp1[0] << "\n" << endl;
return 1;
};