0

I'm relatively new to cpp, so I'm not 100% sure with what I wrote here. I did some research on my error and got to a dead end.

I have a header file (I removed some of the unnecessary code to simplify):

class data_collector {
public:
    data_collector();
    data_collector(RestController restController , data_handler handler);   //constructor
    void intHandler(int dummy);
    static void static_intHandler(int dummy);

private:
    static data_collector instance;
}

the cpp file use the intHandler this way:

struct data_collector data_collector::instance;

data_collector::data_collector(RestController restController, data_handler handler) //constructor
{
    signal(SIGINT, data_collector::static_intHandler);
}
void data_collector::intHandler(int dummy)
{
    keepRunning = false;
    pcieio_stop_channel(&dma_channel.dcd);
}
void data_collector::static_intHandler(int dummy)
{
    data_collector::instance.intHandler(dummy);
}
   signal(SIGINT, data_collector::static_intHandler);

and I recieve /projects/drive/DataServiceRT/./RFSoCControl/data_collector.h:168: undefined reference to `data_collector::instance' collect2: error: ld returned 1 exit status Makefile:3: recipe for target 'test' failed when i try to compile and run the makefile.

I don't really know what I am doing wrong here, so if you have ideas I would appreciate it

Meyer Buaharon
  • 425
  • 5
  • 15
  • Does this answer your question? [static variable link error](https://stackoverflow.com/questions/9282354/static-variable-link-error) – fabian Oct 03 '21 at 07:23
  • @fabian I reviewed the answer you've sent me, and changed the code accordingly, but I still receive the same error. I will edit the question with the new code. – Meyer Buaharon Oct 03 '21 at 07:53
  • I saw that in the .cpp you defined `struct data_collector`. Have you tried to remove the `struct` keyword? – dlivshen Oct 03 '21 at 08:06
  • Could you try to build a [mre] so that we can easily reproduce your problem? – Serge Ballesta Oct 03 '21 at 08:18
  • @dlivshen: Is is probably the cause: if a class is declared as `class` somewhere, it should always be refered using `class` (or nothing) but never with `struct`. – Serge Ballesta Oct 03 '21 at 08:33

0 Answers0