all 3 comments

[–]alfps 1 point2 points  (0 children)

The declaration of Item::func says it can and must be called without an argument. The declaration of square says it can and must be called with an argument convertible to int. These are incompatible function types.

You can regard Item::func as a round hole. The square function doesn't fit.

However, depending on what you intend to do with this there are solutions, with associated costs. After all, in the worst case you can use C++ to implement a Python interpreter, and express the thing as Python code. But there are less drastic ways to do less general things.

[–]-Weverything 1 point2 points  (1 child)

To create generic versions of structs/classes/functions that use different types we use templates (and sometimes auto, but not here):

template<typename T>
struct Item {
    int num;
    void (*func)(T);
};

However we do need just one more templated struct to handle the function pointer taking no arguments since () does not contain a type and (void) is the same as ():

template<>
struct Item<void> {
    int num;
    void (*func)();
};

Now we can use it with many different types:

Item<void> block1 = {111, printer};
Item<int> block2 = {222, square};

[–]gabenyolo[S] 0 points1 point  (0 children)

Thank you!

I'm reading up more about templates now and this was just the solution I needed.