Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Initialize static array in C++

I have these classes:

class FS{
  static char mount(Partition* p)
      {return myImpl->mount(p);}
  /*...*/
  KernelFS* myImpl;
};

class KernelFS{
char mount(Partition* p){
   /*...*/
   while(available[i]) i++;
}
  /*...*/
  static bool available[26];
};

Main program only uses static functions from FS, e.g:

void main(){
  Partition* p=/*...*/;
  FS::mount(p);
  /*...*/
}

When FS::mount(p) is called, it calls myImpl->mount(p) (which is a function from KernelFS class). And here's the problem. When it comes to

while(available[i]) i++;

...it breaks! I think the problem is that I haven't initialized the array available[26], and I have no idea how to do that... What else can be the problem? Please help.

Btw, main() never creates FS or KernelFS objects, so I think that there is no use of constructors...

like image 427
vtomic85 Avatar asked Dec 07 '22 16:12

vtomic85


1 Answers

You need to define it in (exactly)one of your cpp files:

bool KernelFS::available[] = {0};
like image 79
Alok Save Avatar answered Dec 22 '22 19:12

Alok Save