jankoritak
jankoritak

Reputation: 595

C++ passing pointer to a function error

I've been having some trouble trying to solve how to save pointer to a function into a variable.

Basically I have a priority queue, which consists of this type of structures:

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

struct actRecord{
  double actTime;
  int prio;
  eventPointer eventPtr;
};

What I want to do is call function Calendar::CalendarPush() in Main.cpp, pass it arguments time, priority and a pointer to a method of class (rather object) Transaction::Event1().

But I'm still getting an error:

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

Whole code:

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

int main() {
    cout << "Inicializuji" << endl;
    double time = 0.0;
    int priority = 0;
    calendar cal;
    transaction tran;

    cout << "Planuji prvni event" << endl;

    cal.calendarPush(time, priority, &transaction::event1);

    return 0;
}

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

class calendar {
    private:
            typedef void (transaction::*eventPointer) ();

            struct actRecord{
                    double actTime;
                    int prio;
                    eventPointer eventPtr;
            };

            std::priority_queue<actRecord> cal;

    public:
            calendar();
            ~calendar();

            bool calendarEmpty();
            void calendarPush(double, int, eventPointer);
            void calendarPop();
            actRecord calendarTop();
};

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

bool calendar::calendarEmpty() {
    return cal.empty();
}

void calendar::calendarPush(double time, int priority, eventPointer eve) {

    actRecord record;

    record.actTime = time;
    record.prio = priority;
    record.eventPtr = eve;

    cal.push(record);
}

calendar::actRecord calendar::calendarTop() {

    return cal.top();
}

void calendar::calendarPop() {

    cal.pop();
}

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

class transaction {

    typedef void (*eventPointer) ();

    struct actRecord{
            double actTime;
            int prio;
            eventPointer eventPtr;
    };

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

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

void transaction::event1() {

    return;
}

void transaction::event2() {

    return;
}

void transaction::event3() {

    return;
}

void transaction::event4() {

    return;
}

Upvotes: 0

Views: 386

Answers (1)

BartoszKP
BartoszKP

Reputation: 35891

There are two problems in your code:

  • in main you should pass a pointer to a member function like this: cal.calendarPush(time, priority, &transaction::event1);

  • you define two eventPointer types - one in calendar, and one in transaction. Your method calendarPush accepts a pointer to calendar's member not to transaction's member. Make up your mind on this one. For your current code to compile the line in main should read: cal.calendarPush(time, priority, &calendar::calendarPop); for example, but I'm guessing it's not what you wanted.

If you want to change to transaction:

  • make sure transaction is defined before calendar (like including transaction.h in calendard.h)

  • use this in calendar: typedef void (*transaction::eventPointer) ();

  • look at first point above

And what would be better: try reading about std::function.

Upvotes: 2

Related Questions