C++ Pointer to a function error

Hello, I've been having a problem I can't seem to solve on ym own whole day. I'm really new to C++ so I hope you can give me a hand.

I have a class Calendar which has an attribute of priority queue, that accepts records of structure defined as:

1
2
3
4
5
6
7
typedef void (Calendar::*eventPointer)();

struct activationRecord {
	double Time;
	int Priority;
	eventPointer activationEvent;
};


And here is the problem. Whole day I've been trying to fill the Calendar with some test entries by calling the method

 
void Calendar::calendarPush(double Time, int Priority, eventPointer event)


This is how I call it

 
calendar.calendarPush(Time, Priority, &Calendar::calendarEmpty);


But Visual Studio keeps to warn me with this error

 
argument of type "bool (Calendar::*)()" is incompatible with parameter of type "eventPointer *"


Does anyone know what I am doing wrong?

Thank you, I'm starting to be really depressed about this as I've been stuck whole day, unable to solve this.

If it helps, here is a whole CODE

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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87

#include <iostream>

#include "Calendar.h"

using namespace std;

int main() {


	cout << "Initializing ..." << endl;

	double Time = 0.0;
	int Priority = 0;
	Calendar calendar;

	cout << "Pushing first activation record .." << endl;

	calendar.calendarPush(Time, Priority, &Calendar::calendarEmpty);

}

----------CALENDAR.H------------

#include <iostream>
#include <queue>

using namespace std;

typedef void (Calendar::*eventPointer)();

struct activationRecord {
	double Time;
	int Priority;
	eventPointer activationEvent;
};


bool operator <(const activationRecord & a, const activationRecord & b) {
    return a.Time > b.Time;
}

class Calendar {

private:
	std::priority_queue<activationRecord> activationCalendar;

public:
	bool calendarEmpty();

	void calendarPush(double, int, eventPointer);

	activationRecord calendarTop();
	void calendarPop();

};


----------CALENDAR.CPP----------

#include "Calendar.h"

bool Calendar::calendarEmpty() {

	return activationCalendar.empty();
}

void Calendar::calendarPush(double Time, int Priority, eventPointer event) {

	activationRecord record;

	record.Time = Time;
	record.Priority = Priority;
	record.activationEvent = event;

	activationCalendar.push(record);
}

activationRecord Calendar::calendarTop() {

	return activationCalendar.top();
}

void Calendar::calendarPop() {

	activationCalendar.pop();
}

Last edited on
I have a class Calendar which has an attribute of priority queue, that accepts records of structure defined as:

typedef void (Calendar::*eventPointer)();

This is how I call it:

calendar.calendarPush(Time, Priority, &Calendar::calendarEmpty);

But Visual Studio keeps to warn me with this error

argument of type "bool (Calendar::*)()" is incompatible with parameter of type "eventPointer *"


Notice that the return type for your eventPointer typedef is void while the return type of Calendar::calendarEmpty is bool which is what the diagnostic is indicating.
Last edited on
Thank You for a reply cire,

even if I change the call from

 
calendar.calendarPush(Time, Priority, &Calendar::calendarEmpty);


for example to

 
calendar.calendarPush(Time, Priority, &Calendar::calendarPop);


even though the data types now do correspond, I'm getting an error

 
 argument of type "void (Calendar::*)()" is incompatible with parameter of type "eventPointer *"


in a Main.cpp and one more error in Calendar.cpp on line with

 
record.activationEvent = event;


saying that

 
a pointer to a bound function may only be used to call the function


I feel like I'm the dumbest person on the Earth, since I havent been able to solve this whole day, but I just ..
I think something is different with your code than what you're posting.

 argument of type "void (Calendar::*)()" is incompatible with parameter of type "eventPointer *"


Your Calendar::push signature doesn't take a value of type eventPointer*. It takes a value of type eventPointer.

Does this simple code compile for you?

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

struct A
{
    typedef void (A::*eventType)();

    void eventFunc() {std::cout << "eventFunc()\n";}

    eventType func;
};

int main()
{
    A a;
    a.func = &A::eventFunc;

    (a.*a.func)(); // invoke the pointer with object a
}


You may find http://www.parashift.com/c++-faq/pointers-to-members.html helpful.
Last edited on
Well, i just tried adding

 
class Calendar;


before i define it in Calendar.h, right after the

 
using namespace std;


and it works.

I don't know why, I don't understand why, im totally wtf .. But happy.
Topic archived. No new replies allowed.