Cannot write a function pointer into a class implementation file

I have a MainWindow class with ther .h and cpp files.
Compiler tells me :

argument of type 'void (MainWindow::)(long int)' does not match 'void (*)(long int)'

I have tried all combinations, with class prefix, without it,.... without success.

1
2
3
4
5
6
7
8
void MainWindow::update1(long value){ }

void MainWindow::on_pushButton_6_clicked()
{
    void (*pFunc)(long);
    pFunc = update1;        // here is where I have the error.

}


Any help ? Thanks
Last edited on
1
2
3
4
5
6
7
8
9
struct S{
   void func(int){}
};

int main(){
   void(S::*fp)(int);
   fp = &S::func;
   return 0;
}

Though when dealing with function pointers it is best to typedef them.
For a pointer to a member function, you have to specify the class:
void (MainWindow::*pFunc)(long);
Thanks everybody.
And .... How can I write a function into another class to pass the function pointer???

class A
void(A::*fp)(int);
fp = &A::function1;

classb::myfunction(fp) // ??????


class B
myfunction(void *fp(int)) // ??????

now I have a seriuos problem ....

without typedef:
1
2
3
4
5
//class A
void (A::*fp)(int) = &A::function1;

//class B
myfunction(void(A::*fp)(int));

with typedef:
1
2
3
4
5
6
typedef void(A::*fpointer)(int);
//class A
fpointer fp = &A::function1

//class B
myfunction(fpointer fp);
Thanks hamsterman.
I cant write the correct code. ( I have different errors : cannot convert, matching error ...)

I have to declare typedef void(A::*fpointer)(int); at class B ?
My class A and class B are stored in a different files (with their h and cpp files)
I need more help, excuse me.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>

struct A{
   void func(int i){ std::cout << i; }
};

struct B{
   void func( void(A::*fp)(int) ){
      A a;
      (a.*fp)(5);
   }
};

int main(){
   B b;
   b.func(&A::func);
   std::cin.get();
   return 0;
}

typedef is just for comfortability.
I keep without doing this:

I have
Class A (h and cpp)
Class B (h and cpp)

Class A
1
2
3
4
5
6
7
8
9
10
11
12
13
A::Myfunction1 (long value) {}
Myfunction2 (long value) {}

   
   A::Comunicator_to_B() 
  {
   void (*pFunc1)(long)= A::Myfunction1;
   void (*pFunc2)(long)= Myfunction2;

   classB().caller1(pFunc1);
   classB().caller1(pFunc2);

  }

Class B
void B::caller1( void (*fun_long)(long)) { fun_long(33);}


Ok, pFunc2 works right, but pfunc1 does not becase my compiler tells me there is no match 'void (A::*)(long int)' to 'void (*)(long int)' in initialization
(and any combination gives me no results)

I can work using pFunc2, but now the problem is that form pFunc2 I cannot see A members .

Any Idea to fix my problems ?
Thanks

Last edited on
The type of &Class::Method is not the same as &Function thus you will not be able to pass both into the same function. Also, to call a function pointed by a void(*A::fp)(long) you need an A object.
What you might be missing is that when you have a method
1
2
3
4
5
6
struct A{
   int a;
   void set(int i){
      a = i;
   }
};

you can write a somewhat equivalent function (it generates almost the same machine code)
1
2
3
void set(A*const that, int i){
   that->a = i;
}

Now clearly while a method seems to have only one argument, global equivalent has two. How can that be? Well, it cant. Look how you call them:
1
2
my_a.set(5);
set(&my_a, 5);
Both lines have a "my_a" and "5". Both functions need these two to be called. Thus the thing on the left side of '.' can be considered an argument.

So what you are trying to do is call a two argument function with one argument only.
No, I think you are not understanding the main problem.
If both classes are defined at the same file there is no problem, it works.

But working on different files for each class I have the problem.

As you can see at my last post, I have the solution :
Write my function without class scope :
(that is Myfunction2 instead of A::Myfunction1)
So, I can define and pass it to the external class.
The problem is that now I dont see nothing of my class A . How to fix it ?

I want to write a generic function to accept function pointers of any class.
Thanks
Topic archived. No new replies allowed.