1

I have a large code and I have an error in the middle of it. Here is a simplified version of the parts of the code that has the error.

And this is the error I get:

// Followings are declared in the header

struct Task {
public:
    _COORD p1;
    int p2;
    object p3;
    speed p4;
    bool(Game::*function)(_COORD, int, object, speed);
};


std::vector<Task> tasks;

// Followings are defined in the source

void Game::timer() {
    (some code here)
tasks[i].function(tasks[i].p1, tasks[i].p2, tasks[i].p3, tasks[i].p4);     /*error here*/

expression preceding parentheses of apparent call must have (pointer-to-) function type.

}

void Game::explode(bool(Game::*function)(_COORD, int, object, speed), _COORD p1, int p2, object p3, speed p4) {
    ExplodeTask task;
    task.function = function;
    task.p1 = p1;
    task.p2 = p2;
    task.p3 = p3;
    task.p4 = p4;
    tasks.push_back(task);
}

Does anyone know how to fix it?

2 Answers 2

4

The correct syntax, to call a method function pointer is (objectPtr->*methodPtr)() or (object.*methodPtr)():

void Game::timer() {
    int i = 0;
    ...
    (this->*tasks[i].function)(tasks[i].p1, tasks[i].p2, tasks[i].p3, tasks[i].p4);
}
Sign up to request clarification or add additional context in comments.

Comments

2

What I advice is usage std::function instead function pointer. Std::function syntax is more friendly any function type can be assigned to it from raw functions to lamdba expression. Also it seems a bit strange too me that you are passing p1,p2,p3,p4 as function parameters even there constant. It will be harder to use that way. At least you could override () operator . And call pass the parameters with () operator once that way user wouldn't need pass parameter second time in "timer" function.

If you must use function pointer I think that is better:

struct Task {
public:
    int p1;
    int p2;
    int p3;
    int p4;
    bool operator()()
    {
        return (functionPtr)(p1,p2,p3,p4);
    }
    bool(*functionPtr)(int, int, int, int );
};

Task t { 1, 2 ,3 ,4, &Game::foo(int, int, int, int) };

Than client can make a easy call without passing parameters like. 
t();

so that client of Task class can call task() directly with ease.

IMHO code will be better like :

#include <vector>
#include <functional>

std::vector<std::function<bool(void)>> functionList;

void taskPusher( std::function<bool(int,int,int,int)> foo , int p1, int p2, int p3, int p4)
{

    std::function<bool()> explodeTask = [=]()
    {
        return foo(p1,p2,p3,p4);
    } ;

    functionList.push_back(explodeTask);
}

void explode()
{
    for ( auto& explodeFoo : functionList)
    {
        explodeFoo();
    }
}

3 Comments

Well i didn't know about non of these things! That's why I used pointer to function!
thank you so much for your answer! it was so helpful so i gave it a +1 but the other answer was the exact answer of the question so i accepted that one! But to make sure i was enough appreciateful i went and gave lots of +1s to your recent posts:) i will go learn that functional library!
Thanks a lot. I am happy that you find my answer helpful

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.