How to make void (**)() from void()? [closed] - c++

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 19 days ago.
This post was edited and submitted for review 18 days ago and failed to reopen the post:
Needs details or clarity Add details and clarify the problem by editing this post.
Improve this question
Have function:
void btCallback(esp_spp_cb_event_t event, esp_spp_cb_param_t *param) {
// ...
}
Need to use in:
BT.register_callback(btCallback);
Compiler error:
no known conversion for argument 1 from 'void(esp_spp_cb_event_t, esp_spp_cb_param_t*)' to 'void (**)(esp_spp_cb_event_t, esp_spp_cb_param_t*)'
As I understand it, he needs a pointer to function pointer. I don't know how to create it. I tried a function pointer (through &), does not fit.
Reproduction (PlatformIO / platform: espressif32, board: esp-wrover-kit, framework: arduino):
#include <Arduino.h>
#include <BluetoothSerial.h>
BluetoothSerial BT;
void btCallback(esp_spp_cb_event_t event, esp_spp_cb_param_t *param) {
Serial.println("TEST");
}
void setup() {
Serial.begin(115200);
BT.begin("", true);
BT.register_callback(btCallback);
BT.connect("TEST");
}
void loop() { }
P.S. Is arduino-esp32 BluetoothSerial::register_callback function.

You need to make a pointer variable, and then take a pointer from it using the & operator.
void f()
{
// ...
}
void g(void (**p)())
{
// ...
}
int main()
{
void (*f_ptr)() = f;
g(&f_ptr);
}
Try if here.
As I understand it, he needs a pointer to function pointer. I don't
know how to create it. I tried a function pointer (through &), does
not fit.
In the previous example, taking &f doesn't have any effect. These two lines are equivalent!:
void (*f_ptr)() = f;
void (*f_ptr)() = &f;
Therefore, if you were doing:
g(&f);
you are actually passing a simple function pointer, not a pointer to function pointer.

Related

std::function Through type is not allowed [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I Need to perform a unit test on AddToDB() API. Below is my Attemp but i get error when i create a callback with notify type 2 mention below
typedef std::function<bool(std::string)> strnotify;
typedef std::function<void(strnotify &)> notify;
bool AddToDB(notify data);
// callback for typedef std::function<bool(std::string)> strnotify;
bool StringNotify(std::string){
std::cout<<"String success";
return true;
}
// callback for typedef std::function<void(strnotify &)> notify;
void StringObject(strnotify & obj){ // i get error here as type is not allowed
std::cout<<"string obj success";
}
//Call to API
AddToDB(StringObject)
I get error as type is not allowed.
Yes, your code is work well, like say Chris Dodd, but only AddToBase function. You cannot create function for std::function<void(strnotify &)> notify, because you use reference to std::functions<Type> object.
This code work fine:
using strnotify = std::function<bool(std::string)>;
using notify = std::function<void(strnotify& )>;
bool AddToDB(notify data) {
// do something...
return true;
}
int main() {
auto fun = [](strnotify& s) -> void { /*do something */};
auto fun2 = [](std::string s) -> bool {};
//fun(fun2); // now work :(
AddToDB(fun); // work!
return 0;
}
But if you uncomment line in main function, you get error:
gcc 10.2:
48:13:
no match for call to '(main()::<lambda(strnotify&)>) (main()::<lambda(std::string)>&)'
So, how we can fix that? I have two ways for you:
do not use reference: std::function<void(strnotify)> notify;
firstly create strnotify object and use into function. Example in godbolt.
And use using preferred than typedef please! Good luck!

'variable': initialized auto or register variable not allowed at function scope in 'naked' function [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
int __declspec(naked) Testing(int _byte, int _byte_size) {
int byte_size = _byte_size;
BYTE* bytec = new BYTE[byte_size];
memcpy(reinterpret_cast<void*>(bytec),
reinterpret_cast<void*>(_byte), byte_size);
int RE_format = 0;
}
This is whats cause the error, How would I go about fixing this? I have other code that I don't want to disclose but these are the main variables that are causing the error C2489.
Well, the error message is pretty self-explanatory, and the following at least compiles, although I'm not sure what use it is:
int __declspec(naked) Testing(int _byte, int _byte_size) {
int byte_size;
byte_size = _byte_size;
BYTE* bytec;
bytec = new BYTE[byte_size];
memcpy(reinterpret_cast<void*>(bytec),
reinterpret_cast<void*>(_byte), byte_size);
int RE_format;
RE_format = 0;
}
Live demo

Problem With Functions And Printing Function Values in arduino [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
First of all, I'm gonna say thank you to who help me
so recently I was working on a project which is called Green House in Arduino
then I was about to write a function that reads sensor, and a function that prints that value in function1 and I just came up with some problems,
Here is My code
First of all, I just defined every pin and then
written functions
and this is functions and the main code which has problems
void GetState();
void loop() {
// put your main code here, to run repeatedly:
GetState();
PrintState();
delay(2000);
}
void PrintState()
{
Serial.println("TEMP ");
Serial.println(temp);
Serial.println("Rotobate Khak");
Serial.println(soilstate);
Serial.println("Humidity");
Serial.println(hum);
Serial.println("LDR === ");
Serial.println(LDRSTATE);
Serial.print("\n");
}
void GetState()
{
DHT.read11(Sensor);
int LDRSTATE=analogRead(LDR);
return LDRSTATE;
int soilstate=analogRead(soil);
soilstate= map(soilstate,0,1023,100.00,0);
return soilstate;
int temp=DHT.temperature;
return temp;
int hum=DHT.humidity;
return hum;
}
and I get 'temp' was not declared in this scope error
Declare "int temp;" at the top. The same for all other variables. Do not declare the variables in GetState, just use them.
Remove all "return" lines from Getstate(). Just setting "temp=DHT.temperature;" sets the variable and is enough.
Put the GetState() function before loop() (where it is called). Or, if you prefer, you can add a prototype before loop():
void GetState();
void loop() {
...
Think you are quite new to programming. Please try giving prototypes of GetState() and PrintState() functions above their definition. This is because C/C++ compiler assumes that it returns int by default if there isn't any prototype.
Otherwise, you can create a header file and then include that header file in this program.

C++ Using Object with pass function [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I'm trying to build a object with other object and pass the function
Sorry for unclear description
the question is if the TESTobj is someone else object, and I build the new object(Gradebook) and use TESTobj to pass the function, pass the function without the object is no problem but in the object will error
error: no matching function for call to ‘TESTobj::SET(void (GradeBook::*)())’
no known conversion for argument 1 from ‘void (GradeBook::*)()’ to ‘void (*)()’
Here is Example:
void TESTobj::SET(void (*nu)())
{//point the function
this->nu=nu;
}
void TESTobj::Test()
{//implement the function
(*nu)();
}
void GradeBook::Grade()
{//the function I build
cout << "HELLO" <<endl;
}
void GradeBook::Set()
{
OBJ.SET(&GradeBook::Grade);//GotProblemHere
OBJ.Test();
}
GradeBook::Grade is a non-static member function, it needs to operate on an instance of GradeBook. You can't pass it in to a function expecting a plain function pointer.
You could change TESTobj::SET to accept std::function objects, then wrap your member function in a lambda:
void TESTobj::SET(std::function<void()> nu)
void GradeBook::Set()
{
OBJ.SET([this](){Grade();});
OBJ.Test();
}

Pass pointer by reference c++ [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Yes I have read a lot of tutorials and questions and also tried a lot of combinations but it seems not to work.
My goal is not to use dynamic allocation.
My classes look like this:
Pages
Page
PMain:Page
PCam:Page
on my main when I do this:
1.
main:
Page * page;
PCam main;
main.setContext(context);
page = &main;
page->echo();
result: PCam
but when I try to create the instance inside an outside class and point it to page it fails.
2.
pages class:
Pages::Pages(Page*& page, Context& context){
this->context = &context;
PMain main;
main.setContext(*this->context);
main.echo();
// page = &main; <---
}
main:
Page * page;
Pages pages(page, context);
page->echo();
result: Page
expected result: PCam
My classes:
Page:
void Page::setContext(Context & context)
{
this->context = &context;
}
void Page::echo() //virtual
{
std::cout << "echo Page" << std::endl;
}
PMain:
void PMain::echo(){
std::cout << "echo PMain" << std::endl;}
}
PCam:
void PCam::echo(){
std::cout << "echo PCam" << std::endl;}
}
Any help would be appreciated. thanks.
Your problem, or one of them, is that this:
Pages::Pages(Page*& page, Context& context){
[...]
PMain main;
is a local stack variable. When this function returns, it ceases to exist.
If you've assigned it to a pointer, you'll get undefined behavior by using it.
My goal is not to use dynamic allocation.
Unless you have some specific reason, this is a mostly pointless goal. If you want a pointer to a stack object (i.e., one that's not dynamically allocated), that object must remain in scope as long as you use the pointer. If you can't do that, then you need to put it on the heap (i.e., dynamically allocate).