consider these files:
ph:
#ifndef _p_h_ #define _p_h_ class p{ public: static void set_func(int(*)()); private: static int (*sf)(); }; #endif
p.cpp:
#include "ph" #include <cstdio> int (p::*sf)() = NULL; //defining the function pointer void p::set_func(int(*f)()){ sf = f; }
main.cpp:
#include "ph" #include <iostream> int function_x(){ std::cout << "I'm function_x()" << std::endl; return 1234; } int main(){ p::set_func(function_x); }
when compiling, I get the following:
$ g++ -o pp main.cpp p.cpp /tmp/ccIs0M7r.o:p.cpp:(.text+0x7): undefined reference to `p::sf' collect2: ld returned 1 exit status
a
$ g++ -c -o pp p.cpp
compiles correctly.
What is wrong with the code? I just canβt find where the problem is, please, your help will be more than appreciated.
Thanks.
source share