1

I'm running into what seems like a syntax problem. Looking at the other StackOverflow answer doesn't give me an answer applicable to my problem. At least not one which I can understand.

My scheduler class:

#define MAX_TASKS 10

typedef struct taskProps {
    int interval;
    int elapsedTime;
    int (Controller::*taskFunction)(void);
} taskProps;

class TaskScheduler {
public:
    TaskScheduler();
    int setUpdateInterval(int interval);
    int addTask(int interval, int (Controller::*taskFunction)(void));
    int startTimer();
    void clearTasks();
    int checkTasks();

private:
    int numberOfTasks;
    int updateInterval;
    taskProps scheduledTasks[MAX_TASKS];
};

This all compiles fine, but the problem lays within call the member function pointer in this function:

int TaskScheduler::checkTasks(){
    int tasksExecuted = 0;

    for(int i = 0; i < numberOfTasks; i++){
        if(scheduledTasks[i].elapsedTime >= scheduledTasks[i].interval){
            scheduledTasks[i].taskFunction;
            scheduledTasks[i].elapsedTime = 0;
            tasksExecuted++;
        }

        scheduledTasks[i].elapsedTime += updateInterval;
    }

    return tasksExecuted;
}

Compiling this gives me the error;

../Core/Src/TaskScheduler.cpp:88:22: warning: statement has no effect [-Wunused-value]

Other tries:

scheduledTasks[i].*taskFunction;
../Core/Src/TaskScheduler.cpp:88:23: error: 'taskFunction' was not declared in this scope


scheduledTasks[i].taskFunction();
../Core/Src/TaskScheduler.cpp:88:35: error: must use '.*' or '->*' to call pointer-to-member function in '((TaskScheduler*)this)->TaskScheduler::scheduledTasks[i].taskProps::taskFunction (...)', e.g. '(... ->* ((TaskScheduler*)this)->TaskScheduler::scheduledTasks[i].taskProps::taskFunction) (...)'

Anyone able to help me out and explain what piece of knowledge I'm missing here?

4
  • This gives the same error: ../Core/Src/TaskScheduler.cpp:88:24: error: 'taskFunction' was not declared in this scope. That's why I posted this... I hate it got deleted Commented Mar 25, 2021 at 13:57
  • 1
    I've reopend the question as your problem is a little more complex then the dupe takes care of. I'll be adding an answer in a moment. Commented Mar 25, 2021 at 14:13
  • 1
    i can understand your frustration, but note that a question getting closed is something temporarily, there are mechanics in place to reopen the question after a review, or it can be reopened by voting. Deleting a question is different Commented Mar 25, 2021 at 14:20
  • 1
    Yes, I didn't know that hence why I opened a new one. Learned something today! Commented Mar 25, 2021 at 14:30

1 Answer 1

3

When you want to call a member function pointer the syntax you use is

(object_of_type_mem_func_pointer_points_to.*function_pointer)(parameters)

or

(pointer_to_object_of_type_mem_func_pointer_points_to->*function_pointer)(parameters)

Unfortunately, (scheduledTasks[i].*taskFunction)() isn't going to work, as taskFunction requires a Controller object to call the taskFunction on. That needs code more like this:

(controller_object.*(scheduledTasks[i].taskFunction))()

or

(pointer_to_controller_object->*(scheduledTasks[i].taskFunction))()
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for reconsidering my question, this fixed my problem AND made me understand the problem! Very clear explanation.
@MrEmper No problem. Sorry for the early close but we got you there :)

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.