如何从类构造函数中调用非成员函数

时间:2016-03-24 05:07:03

标签: c++ qt oop

我有一个C代码函数,我需要从类构造函数调用它。

Class a{
.....
}
//Included the header file in the class and
//declared the function globally as extern 
extern void ab(void); //function available in the c code 
a::a() //constructor of class a
{
::ab(); //calling the non member function - giving an error
}

它给出了一个错误:对'ab()'

的未定义引用

有人可以帮我解决这个问题吗?

2 个答案:

答案 0 :(得分:1)

简单地从提供的代码和错误判断,看起来好像你没有定义你的功能。您已声明它,但编译器抱怨它无法找到该函数的实现。要解决此问题,请为您的功能提供实施,例如

void ab(void)
{
    int x = 0;
    printf("My int: %d", x);
}

答案 1 :(得分:-2)

将指针作为参数发送到构造函数:

a::a(void (*ptrFun)(void) )
{
     ptrFun();
}
相关问题