C++ pass reference to function inside same class
I am developing an embedded system with the mbed framework in C++.
To attach an interrupt function to the serial interrupt, I normally do this:
Serial pc(pin_u_tx, pin_u_rx,115200);
void SerialStart(void) {
...
pc.attach(&SerInt);
...
}
void SerInt(){
...
}
But now I need to do the same thing from inside a class, and it doesn't work as I can't refer to an internal function:
CTCOMM::CTCOMM()
{
pc = new Serial(ser_tx, ser_rx, ser_baud);
pc->attach(&serial_interrupt);
}
void CTCOMM::serial_interrupt() {
...
}
I tried a few ways, but none works:
pc->attach(&serial_interrupt);
gives the error
libCTcommctcomm.cpp:12:17: error: ISO C++ forbids taking the address of an unqualified or parenthesized non-static member function to form a pointer to member function. Say '&CTCOMM::serial_interrupt' [-fpermissive]
pc->attach(*serial_interrupt);
gives the error
libCTcommctcomm.cpp:12:17: error: invalid use of member function 'void CTCOMM::serial_interrupt()' (did you forget the '
pc->attach(*serial_interrupt());
gives the error
libCTcommctcomm.cpp:12:33: error: void value not ignored as it ought to be ()' ?)
pc->attach((*this)->*(serial_interrupt));
gives the error
libCTcommctcomm.cpp:12:23: error: invalid use of non-static member function 'void CTCOMM::serial_interrupt()'
and so on (I tried more suggestions found here, but got no succes).
What would be the correct way to point to that function?
c++ class pointers mbed
add a comment |
I am developing an embedded system with the mbed framework in C++.
To attach an interrupt function to the serial interrupt, I normally do this:
Serial pc(pin_u_tx, pin_u_rx,115200);
void SerialStart(void) {
...
pc.attach(&SerInt);
...
}
void SerInt(){
...
}
But now I need to do the same thing from inside a class, and it doesn't work as I can't refer to an internal function:
CTCOMM::CTCOMM()
{
pc = new Serial(ser_tx, ser_rx, ser_baud);
pc->attach(&serial_interrupt);
}
void CTCOMM::serial_interrupt() {
...
}
I tried a few ways, but none works:
pc->attach(&serial_interrupt);
gives the error
libCTcommctcomm.cpp:12:17: error: ISO C++ forbids taking the address of an unqualified or parenthesized non-static member function to form a pointer to member function. Say '&CTCOMM::serial_interrupt' [-fpermissive]
pc->attach(*serial_interrupt);
gives the error
libCTcommctcomm.cpp:12:17: error: invalid use of member function 'void CTCOMM::serial_interrupt()' (did you forget the '
pc->attach(*serial_interrupt());
gives the error
libCTcommctcomm.cpp:12:33: error: void value not ignored as it ought to be ()' ?)
pc->attach((*this)->*(serial_interrupt));
gives the error
libCTcommctcomm.cpp:12:23: error: invalid use of non-static member function 'void CTCOMM::serial_interrupt()'
and so on (I tried more suggestions found here, but got no succes).
What would be the correct way to point to that function?
c++ class pointers mbed
I think you don't have a choice here, as you need a pointer to a function. The only way is to use a static method.
– Matthieu Brucher
Nov 25 '18 at 19:59
What is the signature ofattach
? Can you modify it to use, e.g.,std::invoke
to call a callback function? Then you could passthis
as the first argument to call a member function, something likeattach(&CTCOMM::serial_interrupt, this)
.
– Evg
Nov 25 '18 at 20:06
search for "pointer to member function"
– Kenny Ostrom
Nov 25 '18 at 20:45
add a comment |
I am developing an embedded system with the mbed framework in C++.
To attach an interrupt function to the serial interrupt, I normally do this:
Serial pc(pin_u_tx, pin_u_rx,115200);
void SerialStart(void) {
...
pc.attach(&SerInt);
...
}
void SerInt(){
...
}
But now I need to do the same thing from inside a class, and it doesn't work as I can't refer to an internal function:
CTCOMM::CTCOMM()
{
pc = new Serial(ser_tx, ser_rx, ser_baud);
pc->attach(&serial_interrupt);
}
void CTCOMM::serial_interrupt() {
...
}
I tried a few ways, but none works:
pc->attach(&serial_interrupt);
gives the error
libCTcommctcomm.cpp:12:17: error: ISO C++ forbids taking the address of an unqualified or parenthesized non-static member function to form a pointer to member function. Say '&CTCOMM::serial_interrupt' [-fpermissive]
pc->attach(*serial_interrupt);
gives the error
libCTcommctcomm.cpp:12:17: error: invalid use of member function 'void CTCOMM::serial_interrupt()' (did you forget the '
pc->attach(*serial_interrupt());
gives the error
libCTcommctcomm.cpp:12:33: error: void value not ignored as it ought to be ()' ?)
pc->attach((*this)->*(serial_interrupt));
gives the error
libCTcommctcomm.cpp:12:23: error: invalid use of non-static member function 'void CTCOMM::serial_interrupt()'
and so on (I tried more suggestions found here, but got no succes).
What would be the correct way to point to that function?
c++ class pointers mbed
I am developing an embedded system with the mbed framework in C++.
To attach an interrupt function to the serial interrupt, I normally do this:
Serial pc(pin_u_tx, pin_u_rx,115200);
void SerialStart(void) {
...
pc.attach(&SerInt);
...
}
void SerInt(){
...
}
But now I need to do the same thing from inside a class, and it doesn't work as I can't refer to an internal function:
CTCOMM::CTCOMM()
{
pc = new Serial(ser_tx, ser_rx, ser_baud);
pc->attach(&serial_interrupt);
}
void CTCOMM::serial_interrupt() {
...
}
I tried a few ways, but none works:
pc->attach(&serial_interrupt);
gives the error
libCTcommctcomm.cpp:12:17: error: ISO C++ forbids taking the address of an unqualified or parenthesized non-static member function to form a pointer to member function. Say '&CTCOMM::serial_interrupt' [-fpermissive]
pc->attach(*serial_interrupt);
gives the error
libCTcommctcomm.cpp:12:17: error: invalid use of member function 'void CTCOMM::serial_interrupt()' (did you forget the '
pc->attach(*serial_interrupt());
gives the error
libCTcommctcomm.cpp:12:33: error: void value not ignored as it ought to be ()' ?)
pc->attach((*this)->*(serial_interrupt));
gives the error
libCTcommctcomm.cpp:12:23: error: invalid use of non-static member function 'void CTCOMM::serial_interrupt()'
and so on (I tried more suggestions found here, but got no succes).
What would be the correct way to point to that function?
c++ class pointers mbed
c++ class pointers mbed
asked Nov 25 '18 at 19:53
MGGMGG
5319
5319
I think you don't have a choice here, as you need a pointer to a function. The only way is to use a static method.
– Matthieu Brucher
Nov 25 '18 at 19:59
What is the signature ofattach
? Can you modify it to use, e.g.,std::invoke
to call a callback function? Then you could passthis
as the first argument to call a member function, something likeattach(&CTCOMM::serial_interrupt, this)
.
– Evg
Nov 25 '18 at 20:06
search for "pointer to member function"
– Kenny Ostrom
Nov 25 '18 at 20:45
add a comment |
I think you don't have a choice here, as you need a pointer to a function. The only way is to use a static method.
– Matthieu Brucher
Nov 25 '18 at 19:59
What is the signature ofattach
? Can you modify it to use, e.g.,std::invoke
to call a callback function? Then you could passthis
as the first argument to call a member function, something likeattach(&CTCOMM::serial_interrupt, this)
.
– Evg
Nov 25 '18 at 20:06
search for "pointer to member function"
– Kenny Ostrom
Nov 25 '18 at 20:45
I think you don't have a choice here, as you need a pointer to a function. The only way is to use a static method.
– Matthieu Brucher
Nov 25 '18 at 19:59
I think you don't have a choice here, as you need a pointer to a function. The only way is to use a static method.
– Matthieu Brucher
Nov 25 '18 at 19:59
What is the signature of
attach
? Can you modify it to use, e.g., std::invoke
to call a callback function? Then you could pass this
as the first argument to call a member function, something like attach(&CTCOMM::serial_interrupt, this)
.– Evg
Nov 25 '18 at 20:06
What is the signature of
attach
? Can you modify it to use, e.g., std::invoke
to call a callback function? Then you could pass this
as the first argument to call a member function, something like attach(&CTCOMM::serial_interrupt, this)
.– Evg
Nov 25 '18 at 20:06
search for "pointer to member function"
– Kenny Ostrom
Nov 25 '18 at 20:45
search for "pointer to member function"
– Kenny Ostrom
Nov 25 '18 at 20:45
add a comment |
1 Answer
1
active
oldest
votes
Try this.
pc->attach(callback(this, &CTCOMM::serial_interrupt));
pc->attach(this, &CTCOMM::serial_interrupt);
should also work. But it is deprecated in the recent versions of mbed OS.
Here is the latest Mbed API:
https://os.mbed.com/docs/v5.10/mbed-os-api-doxy/classmbed_1_1_serial.html
First one gives this error:libCTcommctcomm.cpp:13:56: error: cannot convert 'CTCOMM*' to 'unsigned char*' in argument passing
but the second one works, only with a warning that it's deprecated! I'll try to fix the first one, but for now thanks, it works.
– MGG
Nov 26 '18 at 9:47
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53471303%2fc-pass-reference-to-function-inside-same-class%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Try this.
pc->attach(callback(this, &CTCOMM::serial_interrupt));
pc->attach(this, &CTCOMM::serial_interrupt);
should also work. But it is deprecated in the recent versions of mbed OS.
Here is the latest Mbed API:
https://os.mbed.com/docs/v5.10/mbed-os-api-doxy/classmbed_1_1_serial.html
First one gives this error:libCTcommctcomm.cpp:13:56: error: cannot convert 'CTCOMM*' to 'unsigned char*' in argument passing
but the second one works, only with a warning that it's deprecated! I'll try to fix the first one, but for now thanks, it works.
– MGG
Nov 26 '18 at 9:47
add a comment |
Try this.
pc->attach(callback(this, &CTCOMM::serial_interrupt));
pc->attach(this, &CTCOMM::serial_interrupt);
should also work. But it is deprecated in the recent versions of mbed OS.
Here is the latest Mbed API:
https://os.mbed.com/docs/v5.10/mbed-os-api-doxy/classmbed_1_1_serial.html
First one gives this error:libCTcommctcomm.cpp:13:56: error: cannot convert 'CTCOMM*' to 'unsigned char*' in argument passing
but the second one works, only with a warning that it's deprecated! I'll try to fix the first one, but for now thanks, it works.
– MGG
Nov 26 '18 at 9:47
add a comment |
Try this.
pc->attach(callback(this, &CTCOMM::serial_interrupt));
pc->attach(this, &CTCOMM::serial_interrupt);
should also work. But it is deprecated in the recent versions of mbed OS.
Here is the latest Mbed API:
https://os.mbed.com/docs/v5.10/mbed-os-api-doxy/classmbed_1_1_serial.html
Try this.
pc->attach(callback(this, &CTCOMM::serial_interrupt));
pc->attach(this, &CTCOMM::serial_interrupt);
should also work. But it is deprecated in the recent versions of mbed OS.
Here is the latest Mbed API:
https://os.mbed.com/docs/v5.10/mbed-os-api-doxy/classmbed_1_1_serial.html
answered Nov 25 '18 at 23:49
Kentaro OkudaKentaro Okuda
5214
5214
First one gives this error:libCTcommctcomm.cpp:13:56: error: cannot convert 'CTCOMM*' to 'unsigned char*' in argument passing
but the second one works, only with a warning that it's deprecated! I'll try to fix the first one, but for now thanks, it works.
– MGG
Nov 26 '18 at 9:47
add a comment |
First one gives this error:libCTcommctcomm.cpp:13:56: error: cannot convert 'CTCOMM*' to 'unsigned char*' in argument passing
but the second one works, only with a warning that it's deprecated! I'll try to fix the first one, but for now thanks, it works.
– MGG
Nov 26 '18 at 9:47
First one gives this error:
libCTcommctcomm.cpp:13:56: error: cannot convert 'CTCOMM*' to 'unsigned char*' in argument passing
but the second one works, only with a warning that it's deprecated! I'll try to fix the first one, but for now thanks, it works.– MGG
Nov 26 '18 at 9:47
First one gives this error:
libCTcommctcomm.cpp:13:56: error: cannot convert 'CTCOMM*' to 'unsigned char*' in argument passing
but the second one works, only with a warning that it's deprecated! I'll try to fix the first one, but for now thanks, it works.– MGG
Nov 26 '18 at 9:47
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53471303%2fc-pass-reference-to-function-inside-same-class%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
I think you don't have a choice here, as you need a pointer to a function. The only way is to use a static method.
– Matthieu Brucher
Nov 25 '18 at 19:59
What is the signature of
attach
? Can you modify it to use, e.g.,std::invoke
to call a callback function? Then you could passthis
as the first argument to call a member function, something likeattach(&CTCOMM::serial_interrupt, this)
.– Evg
Nov 25 '18 at 20:06
search for "pointer to member function"
– Kenny Ostrom
Nov 25 '18 at 20:45