I have a class Array
which has the following constructor:
explicit Array(int size = 0, const T &v = T()) {
if (size < 0) {
throw ArrayExceptions::InvalidLength();
}
Array::size = size;
array = new T[size];
insertValueIntoArray(size,v);
}
In some other class DataController
I have:
Array<Data> dataArray;
int dataAmount;
explicit DataController(int length) : dataArray(length), dataAmount(length) {}
But Data.h
does not have a constructor without arguments so the compiler complaints on const T &v = T()
of Array.h
:
error: no matching function for call to 'Data::Data()'
Instead it has the following constructor:
Data(int length) : length(length), /** Other constructor calls ... **/ {}
What should I change in order to make the Array
use the Data(length)
constructor insead of the Data()
?
I can modify all the files.
I have tried to switch it to:
explicit DataController(int length) : dataArray(length, Data(length)), dataAmount(length) {}
but then I get the same error in line:
array = new T[size];
Minimal example:
template<typename T>
class Array {
int size;
T *array;
public:
explicit Array(int size = 0, const T &value = T()) {
Array::size = size;
array = new T[size];
}
~Array() {
delete[] array;
}
};
class Data {
private:
int length;
public:
Data(int length) : length(length) {}
};
class DataController {
private:
Array<Data> dataArray;
int dataAmount;
public:
explicit DataController(int length) : dataArray(length), dataAmount(length) {}
};
Please suggest solutions without using the std
if you can.