favor considerar estos archivos:Referencia no definida al miembro indicador de función estática en C++, ¿qué estoy haciendo mal?
ph:
#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);
}
al compilar, me sale esto:
$ 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
pero:
$ g++ -c -o pp p.cpp
compila derecha.
¿Qué pasa con el código? Simplemente no puedo encontrar dónde está el problema, por favor su ayuda será más que apreciada.
Gracias.
Puede considerar usando [Boost.Function] (http://www.boost.org/doc/libs/1_47_0/doc/html/function.html). –