Assigning function to a class's events

Hello!
i'm trying to compile this code (this is a real code adaptation)...

file: A.hpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
class  A
{
public:
  A(void (*aEvent) (A *, char)):
    event_A(aEvent) 
  { 
    //... 
  };

  ~A() {};

  void (*publicEvent_A) (A *);
private:
  void (*event_A) (A *, char);

  void myProcedure () 
  {
    event_A (this, 'x');

    // code...

    if (publicEvent_A)
      publicEvent_A (this);
  }
};

class B
{
public:
  B():
    myA(event_B)
  {
    //myA = new A(event_B);
    myA->publicEvent_A = myProcedure_B;
  };

  ~B();
private:
  A *myA; 
  void event_B (A *p1, char p2);
  void myProcedure_B (A *sender)
  {
    sender = sender; //do anything
  };
};


The errors are (bold lines in the code):
main.cpp: In constructor ‘B::B()’:
A.hpp:31: error: argument of type ‘void (B::)(A*, char)’ does not match ‘A*’
A.hpp:34: error: argument of type ‘void (B::)(A*)’ does not match ‘void (*)(A*)’


I know what it means, but suppose the B class doesn't exist in the "A.hpp" file and i write a "main.cpp" file like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include "A.hpp"

void constructorEvent (A *sender, char c)
{
  // code ...
}

void publicEvent (A *sender)
{
  // code ...
}

int main ()
{
  A *myA = new A (constructorEvent);
  myA->publicEvent_A = publicEvent;
}


In this case, the code compile and run as well.
Then, i don't know how to get the first structure of classes (A & B), cause that is what i need. The public and private declarations are important.

i don't know if i explain good, so, for more information: i'm here!

Thanks a lot!!!
Last edited on
Shouldn't 'myA(event_B)' be replaced by 'myA = new A(event_B)' ?
myA(event_B) has the same behavior of myA = new A(event_B).
I tested both ways and the errors are the same. Thats why i wrote the lines 31 and 33 (commented).
Last edited on
perhaps declare event_B as:

void (*event_B)(A *p1, char p2);
Because you're passing in a this pointer, I'd bet this isn't supposed to be a member function. In which case, you really should define it outside of a class and use that typedef within both classes.
Last edited on
Topic archived. No new replies allowed.