I have to add further functions to an old project. This project have to use c++98.
If I try to add a callback function from within another class:
include <stdint.h>
#include <iostream>
typedef void (*cbFuncPtr)(uint32_t, uint32_t*, uint8_t);
namespace n1 {
namespace n2 {
class lld
{
public:
void reg(cbFuncPtr cb)
{
_cb = cb;
};
uint32_t exe()
{
uint32_t result;
if(nullptr != _cb)
{
_cb(0, &result, 1);
}
return(result);
};
private:
cbFuncPtr _cb;
};
}
}
n1::n2::lld inst1;
namespace n1 {
class lla
{
public:
uint32_t getTime()
{
inst1.reg(&lla::callback);
return inst1.exe();
};
private:
void callback(uint32_t arg1, uint32_t* res, uint8_t arg2)
{
*res = arg1 + arg2;
};
};
}
using namespace std;
int main()
{
cout<<"Start"<<"\n";
n1::lla myTime;
cout<<"Result: "<< myTime.getTime() << "\n";
cout<<"Finish"<<"\n";
return 0;
}
I'm getting the following compiler error:
error: cannot convert ‘void (n1::lla::*)(uint32_t, uint32_t*, uint8_t)’ {aka ‘void (n1::lla::*)(unsigned int, unsigned int*, unsigned char)’} to ‘cbFuncPtr’ {aka ‘void (*)(unsigned int, unsigned int*, unsigned char)’}
implementation in an online compiler
I cannot modify the software architecture because this project is a mess.