invalid use of non-static member function

Hello,
i can't figure out the error. Appreciate your advise:
error:
src/mqqt.cpp: In member function 'int AWS_MQQT::aws_mqqt_setup(...)':
src/mqqt.cpp:37:51: error: invalid use of non-static member function
while (subscribe(topic_name, mySubCallBackHandler) != 0) {
^

I removed parameters from functions to simplify the code:

< ======== AWS_IOT ======== >
typedef void (*pSubCallBackHandler_t)(char *topicName, int payloadLen, char *payLoad);

class AWS_IOT{

private:

public:
int connect(...);
int publish(...);
int subscribe(const char *subTopic, pSubCallBackHandler_t pSubCallBackHandler);
};

< ======= AWS_MQQT ==========>
class AWS_MQQT : public AWS_IOT {

private:
void mySubCallBackHandler (char *topicName, int payloadLen, char *payLoad);
public:
int aws_mqqt_setup(...);
};

int AWS_MQQT::aws_mqqt_setup(...) {
...
while ( connect(...) != 0 ) { ... }
...
while (subscribe(topic_name, mySubCallBackHandler) != 0) { ... }
}

<======== main.cpp ====== >

main.cpp:
...
AWS_MQQT* mqqt = new AWS_MQQT;
(*mqqt).aws_mqqt_setup(...);
...


thank you.
How do you set mySubCallBackHandler pointer?
If you assign some member/class function than remember to set this in the following way:
mySubCallBackHandler = &AWS_MQQT::some_function;
a member function operates on an object, which is passed as the this pointer
so you may see the member function void Bar::foo(int n); as void foo(Bar *this, int n);

AWS_MQQT::mySubCallBackHandler() does not respect the signature defined by `pSubCallBackHandler_t' because it has an extra AWS_MQQT parameter (the caller object) and then it can't be used for the `subscribe()'


> I removed parameters from functions to simplify the code
yes, but that code is full of ellipsis that causes other compile errors
your testcase should reproduce the error you are having
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
typedef void (*pSubCallBackHandler_t)(char *topicName, int payloadLen,
                                      char *payLoad);
class AWS_IOT {
private:
public:
  int subscribe(const char *subTopic,
                pSubCallBackHandler_t pSubCallBackHandler);
};

class AWS_MQQT : public AWS_IOT {
private:
  void mySubCallBackHandler(char *topicName, int payloadLen, char *payLoad);

public:
  int aws_mqqt_setup();
};

int AWS_MQQT::aws_mqqt_setup() {
  while (subscribe("", mySubCallBackHandler) != 0) {
  }
}

int main() {
  AWS_MQQT mqqt;
  mqqt.aws_mqqt_setup();
}
@TomCPP:
Thank you for your reply. to your questions:
> How do you set mySubCallBackHandler pointer?
it is defined as :
1
2
3
4
5
6
7
void AWS_MQQT::mySubCallBackHandler (char *topicName, int payloadLen, char *payLoad) {
//	strncpy(rcvdPayload,payLoad,payloadLen);
//	rcvdPayload[payloadLen] = 0;
//	msgReceived = 1;
true;

};


>If you assign some member/class function then remember to set this in the following way:
mySubCallBackHandler = &AWS_MQQT::some_function;

it doesn't, as you see above. But i (think) i understand your point - i missed the pointer type of the function. If i change the call to :
1
2
3
while (subscribe(topic_name, &AWS_MQQT::mySubCallBackHandler) != 0) { 
...
}

it comes back with :
1
2
3
4
5
src/mqqt.cpp: In member function 'int AWS_MQQT::aws_mqqt_setup(const char*, const char*, const char*, const char*, const char*, const char*)':
src/mqqt.cpp:38:62: error: no matching function for call to 'AWS_MQQT::subscribe(const char*&, void (AWS_MQQT::*)(char*, int, char*))'
  while (subscribe(topic_name, &AWS_MQQT::mySubCallBackHandler) != 0) {
 
~  

@ne555:
appreciate your reply.
>. a member function operates on an object, which is passed as this pointer
does it mean that i can't pass the child's member function to the parent's function?.. unless it is "static"?
And if can't, then what is the proper approach ?



>.. yes, but that code is full of ellipsis that causes other compile errors .your test case should reproduce the error you are having

I didn't think of that. Good point. I'll improve on this. thank you!
the prototypes don't match, you can't pass it.

1
2
3
4
5
6
7
8
9
10
11
12
#include <functional>
//function object
typedef std::function<void (char *topicName, int payloadLen, char *payLoad)> pSubCallBackHandler_t;

int AWS_MQQT::aws_mqqt_setup() {
	using namespace std::placeholders; //for _1, _2, _3
	auto callback = std::bind(&AWS_MQQT::mySubCallBackHandler, this, _1, _2, _3);
	//now callback is like a global function that would call this->mySubCallBackHandler(...)
	//so that's what we subscribe
	subscribe("", callback);
	return 0;
}
it would be similar to having an extra void *data parameter on the callback where you would pass the object.
Topic archived. No new replies allowed.