Home | News | Documentation | Download

Variable-sized object may not be initialized

I recognize this is a common problem, but I haven’t found a fix that suit my case. I am trying to create an instance of a class defined in “myClass.h” by using:

  for(Int_t i=0; i<2; i++){
    myClass* T[i]=new myClass();
      
    }

I want to then call T[0] and T[1] in if loops and also use functions (i.e. T[0]->myFunction). But I am getting an error in the for loop:

 variable-sized object may not be initialized
    myClass* T[i]=new myClass();

PS. If I put const Int_t it’s not going to work because i has a range in a loop and not just one fixed value.

Hi @Karl007

You should put declaration of your variable outside the loop:

myClass* T[2];
for(Int_t i=0; i<2; i++){
    T[i]=new myClass();
}

Regards, Sergey

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.