1

I've been having some trouble finding the cause of error in my code and I would like to ask you for help. I already asked about similar problem yesterday but then I thought i found out the cause. The compiler proved me wrong once again this morning ...

The errors say:

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

error C2664: 'Calendar::calendarPush' : cannot convert parameter 3 from 'void (__thiscall Transaction::* )(void)' to 'eventPointer'

the code causing the errors can eb found in

Main.cpp, line (calling of the method)
Calendar::calendarPush(1, 1, &Transaction::event1);

and

Calendar.cpp line (definition of the method)
void Calendar::calendarPush(double Time, int Priority, eventPointer event)

the definition of eventPointer can found in

Calendar.h, line
typedef void (Calendar::*eventPointer) ();

I don't see how are those two incompatible. What's wrong? :/

Here is the code:

----------MAIN.CPP----------

#include <iostream>

#include "Calendar.cpp"
#include "Transaction.cpp"

using namespace std;

int main() {

    cout << "Initializing" << endl;

    double Time = 0.0;
    int Priority = 0;

    cout << "Pushing initial event" << endl;

    Calendar::calendarPush(1, 1, &Transaction::event1);

}

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

#include <iostream>
#include <queue>

using namespace std;

class Calendar;

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

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


bool operator < (const activationRecord & a, const activationRecord & b);

class Calendar {

private:
    static std::priority_queue<activationRecord> activationCalendar;

public:
    bool calendarEmpty();

    static void calendarPush(double, int, eventPointer);

    activationRecord calendarTop();
    void calendarPop();

    void calendarRun();
};


----------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();
}

void Calendar::calendarRun() {

    activationRecord record;

    while(!calendarEmpty()) {
        record = calendarTop();
        calendarPop();

        cout << record.Time << endl;
        cout << record.Priority << endl;
        cout << record.activationEvent << endl << endl; 

    }
}

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

----------TRANSACTION.H----------

#include <iostream>

using namespace std;


class Transaction {

public:
    void event1();
    void event2();
    void event3();
    void event4();
    void event5();

};

----------TRANSACTION.CPP-----------

#include <iostream>

#include "Transaction.h"

using namespace std;

void event1() {

    cout << "event1" << endl;


}

void event2() {

    cout << "event2" << endl;


}

void event3() {

    cout << "event3" << endl;


}

void event4() {

    cout << "event4" << endl;


}

void event5() {

    cout << "event5" << endl;
}

Now all of the sudden Im getting even more errors which make even less sense to me.

error C2027: use of undefined type 'Transaction'

error C2065: 'event1' : undeclared identifier

error C2011: 'Transaction' : 'class' type redefinition

I just don't uderstand how something like this can happen, it seems to in direct disaccord in what my code contains.

I will rather paste whole code again in case i screwed up something i cant see:

----------MAIN.CPP----------

#include <iostream>

#include "Calendar.h"
#include "Transaction.h"

using namespace std;

int main() {

    cout << "Inicializuji" << endl;

    double Time = 0.0;
    int Priority = 0;

    cout << "Vlkadam uvodni udalost" << endl;

    Calendar::calendarPush(1, 1, &Transaction::event1);

}

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

#include <iostream>
#include <queue>

#include "Transaction.h"

using namespace std;

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

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


bool operator < (const activationRecord & a, const activationRecord & b);

class Calendar {

private:
    static std::priority_queue<activationRecord> activationCalendar;

public:
    bool calendarEmpty();

    static void calendarPush(double, int, eventPointer);

    activationRecord calendarTop();
    void calendarPop();

    void calendarRun();
};


----------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();
}

void Calendar::calendarRun() {

    activationRecord record;

    while(!calendarEmpty()) {
        record = calendarTop();
        calendarPop();

        cout << record.Time << endl;
        cout << record.Priority << endl;
        cout << record.activationEvent << endl << endl; 

    }
}

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

----------TRANSACTION.H----------

#include <iostream>

using namespace std;

class Transaction {

public:
    void event1();
};


----------TRANSACTION.CPP-----------

#include "Transaction.h"

using namespace std;

void Transaction::event1() {

}   

Ty for any help.

2
  • "I don't see how are those two incompatible." They are. Pointer-to-member-function-of-class-A is not compatible with Pointer-to-member-function-of-class-B (except when there is a relation between A and B). Commented Dec 1, 2013 at 11:54
  • The problem is, when i change the declaration of eventPointer in Calendar.h to typedef void (Transaction::*eventPointer) ();. The problem still lasts. And as I have to include Transaction.h in Calendar.h I'm getting even more errors like error C2027: use of undefined type 'Transaction', error C2011: 'Transaction' : 'class' type redefinition, followed with the old ones argument of type "void (Transaction::*)()" is incompatible with parameter of type "eventPointer" and error C2664: 'Calendar::calendarPush' : cannot convert parameter 3 from 'void (__cdecl *)(void)' to 'eventPointer'.so confused. Commented Dec 1, 2013 at 12:03

1 Answer 1

3

The problem is because they are pointer to member functions. Although they have the same return type and argument types, they are considered to be different types, because they are members of unrelated classes.

When you specify that your argument is to be of type void (Calendar::*)(), only Calendar's member functions are convertible to it.

One way to solve the problem is for Calendar to take void (*)() instead of void (Calendar::*)(). Then change the event member functions of Transaction to be static functions. The type of the event functions will then be void (*)() instead of void (Transaction::*)().

Another way would be to change eventPointer to be of type void (Transaction::*)() in order for the conversion to work. Also, your Transaction.cpp currently seems to define void event1(); rather than void Transaction::event1(); which will cause some undefined function related errors.

Sign up to request clarification or add additional context in comments.

2 Comments

mpark can you please give an code example of what you mean by " void (*)() "
@Jammi: If we have a function: void f() {}, the type of f is void () and the type of &f (address of f) is void (*)().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.