1

Before using static members I've to initialize them like in this Example.

I would like to do same in my code. But it does not work. GCC is telling me: undefined reference to MainClass::TheStatic1

Code:

#include <iostream>
using namespace std;    

The base class constructor inits 'a' with given Argument

class MyStaticBase {

public:
  MyStaticBase(int iSetA):a(iSetA){};
  ~MyStaticBase(){}
  int a;

  void SayHello(){
    std::cout << "Say Hello from Static Instance: " << a <<  std::endl;
  }    
};

Derived classes init the the Base class with their specific values.

//  First derived Class
class MyStaticDerived1:public MyStaticBase{
public:
  MyStaticDerived1():MyStaticBase(1){  }    
};

// Second derived Class
class MyStaticDerived2:public MyStaticBase{
public:
  MyStaticDerived2():MyStaticBase(2){}    
};

Containing two similar Members, only difference is the constructor call, when they are derived from their base class.

class MainClass {    
public:
 MainClass(){};
 ~MainClass(){};

 static MyStaticDerived1 TheStatic1;
 static MyStaticDerived2 TheStatic2;    
};      

The Main

 int main() {

  MainClass TheMainClass;

  // [PROBLEM]: gcc:undefined reference to `MainClass::TheStatic1'
  TheMainClass.TheStatic1.SayHello();
  TheMainClass.TheStatic2.SayHello();
}

Attempts in main() to solve ( jap, some of them are just guessing )

  // Attempts:
  // MyStaticDerived1::MyStaticDerived1(); // error: cannot call constructor ‘MyStaticDerived1::MyStaticDerived1’ directly [-fpermissive]
  // MainClass::TheStatic1 TheStatic1;   //error: expected ‘;’ before ‘TheStatic1’
  // MainClass::TheStatic2 TheStatic2 = 0;
  // MainClass::TheStatic1();
  // MyStaticDerived1 MainClass::TheStatic1; //qualified-id in declaration before ‘;’ token
  // MyStaticDerived1 MainClass::TheStatic1{}; //qualified-id in declaration before ‘{’ token

Why I'm doing this?

MyStaticDerived are thread handling classes which are called by a signal Handler (SIGCHILD). This handler can only access static Members. Two groups of processes are managed, so I need two similar threadhandling classes.

Cutton Eye
  • 3,207
  • 3
  • 20
  • 39
  • Off topic: Be really careful what you do in a signal handler. Typically all you want to do is set a flag for internal use and get the heck out of dodge. – user4581301 Dec 30 '16 at 03:22
  • 1
    In a source file (global scope, not in a function), you need to define `MainClass::TheStatic1;` and `MainClass::TheStatic2;`. – Thomas Matthews Dec 30 '16 at 03:23
  • Possible duplicate: http://stackoverflow.com/questions/3536372/defining-static-members-in-c – Thomas Matthews Dec 30 '16 at 03:26
  • Possible duplicate:http://stackoverflow.com/questions/185844/initializing-private-static-members – Thomas Matthews Dec 30 '16 at 03:27
  • @ThomasMatthews It's not a duplicate. The examples are about POD, I'm going to init a whole class as a Member. – Cutton Eye Dec 30 '16 at 03:33
  • @ThomasMatthews sis insert your lines right above the main(). But gcc respondet: ‘TheStatic1’ in ‘class MainClass’ does not name a type – Cutton Eye Dec 30 '16 at 03:36
  • buuuuuuut, when you add those cute lines: above main(), it works =). thx Thomas. MyStaticDerived1 MainClass::TheStatic1; MyStaticDerived2 MainClass::TheStatic2; – Cutton Eye Dec 30 '16 at 03:39
  • @user4581301 Jap, know it. It's like a ISR in a microcontroller. I'm just setting a bool in a struct which is a member of the derived class. thx! – Cutton Eye Dec 30 '16 at 03:42

1 Answers1

0

Copy paste code for working example. Thx to @ThomasMatthews and @user4581301 for making this answer possible. See the solution above the main function. Futur informations please find in the links provided by @ThomasMatthews.

#include <iostream>
using namespace std;

class MyStaticBase {
public:
  MyStaticBase(int iSetA):a(iSetA){};
  ~MyStaticBase(){}
  int a;

  void SayHello(){
    std::cout << "Keep a signalhandler SHORT\n"
        << "Just set a flag =)\n"
        << "thx to all!\n"
        << " Instance: " << a <<  std::endl;
  }
};

//  Derived Classes
class MyStaticDerived1:public MyStaticBase{
public:
  MyStaticDerived1():MyStaticBase(1){  }

};
class MyStaticDerived2:public MyStaticBase{
public:
  MyStaticDerived2():MyStaticBase(2){}

};

class MainClass {

public:
 MainClass(){};
 ~MainClass(){};

 static MyStaticDerived1 TheStatic1;
 static MyStaticDerived2 TheStatic2;
};

// [ Solution ]
MyStaticDerived1 MainClass::TheStatic1;
MyStaticDerived2 MainClass::TheStatic2;

int main() {
  MainClass TheMainClass;
  TheMainClass.TheStatic1.SayHello();
  TheMainClass.TheStatic2.SayHello();
}
Cutton Eye
  • 3,207
  • 3
  • 20
  • 39