1

I have my class:

class Exec {
  static Process* procs_table[];

 public:
 Exec(int num, info_init_proc* proc);
};

info_init_proc is a custom struct with information necessary for initializing the Process object

And try to create the constructor as below:

Exec::Exec(int num, info_init_proc* proc) {
   int i;
   for (i = 0; i < num; i++) {
     Exec::procs_table[i] = new Process(proc[i]);
  }
}

It can be compiled, but when i build i get the "Undefined symbol procs_table" error.

What should be modified ?

Sergiu
  • 15
  • 1
  • 4

2 Answers2

4

The immediate problem is that your static array is declared, but not defined. Adding this line to your CPP file will fix that:

Process* Exec::procs_table[SOME_MAX_VALUE];

However, it appears that the choice of static is fundamentally incorrect here, because you do not know n, the size of the allocation, until run-time. In this situation a singleton object with std::vector of process pointers would be more appropriate.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
2

You can't initialize a static member in a constructor. A constructor is creating an instance of the class, yet the static member is shared by all instances.

Here's a great post on static initialization

Community
  • 1
  • 1
AFX
  • 50
  • 5