Having issues when attaching object/method to FunctionPointer

26 Nov 2012

Hi all. I am having trouble attaching my object/method to FunctionPointer. I passed the two objects msExt and cfExt to the exeFunc class. Compilation error: no instance of overloaded function "mbed::FunctionPointer::attach" matches the argument list. That error points to code:_p.attach(_cfExt, _cfExt.checkConfigForFirstStart());

I do have a basic understanding of the meaning to that error(so I think). I have tried a few edits to that line, but I still arrive at compile errors. Is what I am attempting to accomplish not going to work and that I should create a separate function inside the exeFunc class then attach that?

main.cpp code:

#include "mbed.h"
#include "MODSERIAL.h"
#include "ConfigFile.h"
#include "msExtensions.h"
#include "cfExtensions.h"
#include "exeFunc.h"
#include "phSensor.h"

MODSERIAL pc(USBTX, USBRX);

msExtensions msExt  = msExtensions(pc);
cfExtensions cfExt  = cfExtensions(msExt);
phSensor     phS    = phSensor(cfExt, msExt);
exeFunc      exeCmd = exeFunc(msExt, cfExt);

int main()
{
    //cfExt.checkConfigForFirstStart();

    while(1) {

    }
}

exeFunc.h code:

class exeFunc
{

public:
    exeFunc(msExtensions &msExt, cfExtensions &cfExt);


private:

    void _splitFuncFromCmd();
    void _attachCallback();

    msExtensions &_msExt;
    cfExtensions &_cfExt;

    FunctionPointer _p;
};

exeFunc.cpp code exert:

exeFunc::exeFunc(msExtensions &msExt, cfExtensions &cfExt) : _msExt(msExt), _cfExt(cfExt) 
{
    _p.attach(_cfExt, _cfExt.checkConfigForFirstStart());
}
26 Nov 2012

The second example here: http://mbed.org/handbook/Ticker, shows how to attach a functionpointer to a class method.

27 Nov 2012

Erik - wrote:

The second example here: http://mbed.org/handbook/Ticker, shows how to attach a functionpointer to a class method.

I've used ticker before so I though I understood.

The new attach code:

_p.attach(&_cfExt, &cfExtensions::checkConfigForFirstStart);

Also, when I leave the parentheses at the end of the method I receive a new error: a nonstatic member reference must be relative to a specific object. Removing the parentheses fixed that error. Why is that?

27 Nov 2012

If you leave the parentheses afaik you actually try to call the function, so in that case you need to call it on an actual object of that class. But you dont want to call the function, you only want to reference it.

27 Nov 2012

Thank for that explanation. My confusion is stemming from not completely understanding reference and pointers .