Hi,
I am getting the following error while compiling

error: argument of type 'void(FClass::)()' does not match 'void(*)()'

on the line

fPointer=instance.PrintIt; // Give the pointer the function address

This is my code

/**
 * @file FPointer.c
 * This is the main FPointer program start point.
 */

/**
 * Main program function.
 */

#include <stdio.h>
#include <propeller.h>
#include "FClass.c"

int main(void)
{
    FClass instance; // Create an instance of the class
    void (*fPointer)(); // This will point to a function in FClass

    waitcnt(CLKFREQ*3+CNT); // Give the microcontroller 3 secs to boot

    fPointer=instance.PrintIt; // Give the pointer the function address

    return 0;
}

and

/**
 * @file FClass.c
 */

#include <stdio.h>

class FClass
{
    int number; // We will print this number
    public:
        FClass()
        {
            number=8; // Initialize the number
        }
        void PrintIt()
        {
            printf("%d",number); // Print the number
        }
};

Thanks,
Enrique

function pointers can only point to static class methods or global c-style functions. You will have to make PrintIt() a static method if you want to do that.

AD's already got this one covered, but I wanted to throw something in as well.
If you want a static function to use instance specific data, you have to pass a pointer to the instance as a parameter:

You modify your FClass::PrintIt like so:

static void PrintIt(FClass *instance)
{
    printf("%d",instance->number); // Print the number
}

And your fPointer definition to be:

void (*fPointer)(FClass *); // This will point to a function in FClass

And your assignment becomes:

fPointer=FClass::PrintIt; // Give the pointer the function address

To be used like this:

fPointer(&instance);

I learned this when I was combining classes with threading for the first time. (I'm still quite the noob at parallel programming).

Comments
good points :)
This article has been dead for over six months. Start a new discussion instead.