I want to initialize an array of struct type. Currently i am doing this
struct Gene
{
int **boxes;
int fitness;
Gene()
{
boxes = new int*[get_the_number];
for (int i = 0; i < get_the_number; ++i) {
boxes[i] = new int[get_the_number];
}
}
};
and I am initializing like this
Gene * P = new Gene [t];
It is working fine and i can do my work. It calls the default constructor which i have written itself and work is done. But now, i want to pass a parameter into the default constructor. I changed my constructor to this.
Gene(int n)
But I don't know how to initialize it, i did this
Gene * P= new Gene (2) [t];
But it gives me error that "No suitable conversion from Gene* to Gene exists". I know that I can write the setter function and do what I want to do there or instead of making an array of Gene type I can make an array of Gene* and at each index I can initialize new Gene(2)
. But I don't want to do because I don't need to pass a variable. I just wanted to know that can't I call my constructor which takes a parameter.
Can't I do something like Gene * P= new Gene(2)[t];
?
I mean the compiler is calling that constructor which doesn't take a parameter. Can't it call this constructor instead of that? Thank you,
p.s.: I know its a beginners question but I am back to C++ after a long time so I don't quiet remember things.