कृपया पर विचार इन फ़ाइलों:सी ++ में स्थिर फ़ंक्शन पॉइंटर सदस्य के अनिर्धारित संदर्भ, मैं गलत क्या कर रहा हूं?
पीएच:
#ifndef _p_h_
#define _p_h_
class p{
public:
static void set_func(int(*)());
private:
static int (*sf)();
};
#endif
p.cpp:
#include "p.h"
#include <cstdio>
int (p::*sf)() = NULL; //defining the function pointer
void p::set_func(int(*f)()){
sf = f;
}
main.cpp:
#include "p.h"
#include <iostream>
int function_x(){
std::cout << "I'm function_x()" << std::endl;
return 1234;
}
int main(){
p::set_func(function_x);
}
जब संकलन, मैं इस मिल:
$ 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
लेकिन:
$ g++ -c -o pp p.cpp
सही संकलित करता है।
कोड के साथ क्या गलत है? मुझे यह नहीं पता कि समस्या कहां है, कृपया आपकी मदद की सराहना की जाएगी।
धन्यवाद।
आप विचार कर सकते हैं [बूस्ट.फंक्शन] का उपयोग कर (http://www.boost.org/doc/libs/1_47_0/doc/html/function.html)। –