I'm trying to create a dynamic 2D array of objects using the new keyword. This was my initial code, which worked ok.
class Foo{
public:
static Foo ** make2D(int m, int n) {
Foo **a = new Foo*[m];
for (int i = 0; i < m; i++) {
a[i] = new Foo[n];
}
return a;
}
};
int main() {
Foo ** x = Foo::make2D(2, 2);
return 0;
}
Then I decided to add a constructor inside the class. It looks like this:
Foo(int a, int b){}
The rest of the code doesn't change, but now i'm not able to create the 2D array. I'm getting an error on instruction a[i] = new Foo[n];
:
No matching function for call to 'Foo::Foo()'
It seems that the new Foo[n];
instructions calls the constructor, expecting two parameters, and receiving none.
What can I do to fix the error? Maybe I should use something like malloc?