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();
}
Related
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.
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 2 years ago.
Improve this question
myFunction(){
char *tempPath = getenv("LocalAppData");
strcat(tempPath, "\\MS\\namedPipe.json");
printf(" the path is %s \n",tempPath
}
int main(){
myFunction();
myFunction();
return 0;
}
I don't know the second time that I call this function I am getting the path to be appended like
Quoting the man page for getenv:
As typically implemented, getenv() returns a pointer to a string within the environment list. The caller must take care not to modify this string, since that would change the environment of the process.
In other words, what you are currently doing is not allowed.
Instead make another buffer and concatenate in that buffer. Eg:
char *tempPath = getenv("LocalAppData");
if (tempPath != NULL)
{
std::string env;
env = tempPath;
env += "\\MS\\namedPipe.json"
std::cout << env;
}
else
{
std::cout << "No such environment variable\n";
}
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.
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 7 years ago.
Improve this question
I am working on an object-oriented C++ coursework where I need to return error codes from the main function. How would one do this properly?
Unfortunately this is an assessed coursework so I cannot post my code here. But let's say the case is as follows:
I'm building an enigma machine with classes Plugboard, Reflector, and Rotor. I pass each of the configuration files as arguments in the command line. In this task, I'm provided with a file errors.h containing the following:
#define INSUFFICIENT_NUMBER_OF_PARAMETERS 1
#define INVALID_INPUT_CHARACTER 2
#define INVALID_INDEX 3
// and so on...
So I have in my program several functions to check the errors, for example a function to check whether the configuration file contains an invalid character (it has to be 0 to 25). I was thinking of setting this as a boolean function and then in my main function have the following:
if (!plugboard.check_invalid_character(/*some arguments*/)) {
cerr << "Invalid character!" << endl;
return 2;
}
But I'm not completely sure this is the right way to do it? Is it too superficial? Is there a more elegant way of returning error?
I hope my question is a little clearer this time. Thanks before.
You just need to return the value 4 in your main method like this:
int main() {
return 4;
}
Please note that your main function could also have the arguments vector and the argument count so there could be more in the brackets.
If KLibby is right and you use a method with returns the value you need to use something like that:
int doSomething() {
return 4;
}
int main() {
return doSomething();
}
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).