logo elektroda
logo elektroda
X
logo elektroda

[ESP07][Arduino][ESPAsyncWebServer] - I can't move functions to their own classes

sq9etc 753 12
ADVERTISEMENT
Treść została przetłumaczona polish » english Zobacz oryginalną wersję tematu
  • #1 18825738
    sq9etc
    Level 12  
    When I have all the variables and functions relating to the server in the main.c file it is fine. The program compiles and runs.
    Code: C / C++
    Log in, to see the code
    .

    When the variables t , h and index_html[] and functions processor and initWebServer I will move to my own class, then the program stops compiling.
    The following errors appear in the method WebServer::init :
    Code: Bash
    Log in, to see the code
    .

    The code looks as follows:
    Code: C / C++
    Log in, to see the code
    .

    Code: C / C++
    Log in, to see the code
    .

    Code: C / C++
    Log in, to see the code
    .

    What am I doing wrong?
  • ADVERTISEMENT
  • #2 18825915
    khoam
    Level 42  
    The function send () that you use in WebServer::init () has the following declaration:
    Code: C / C++
    Log in, to see the code
    As the fourth parameter it expects a function type AwsTemplateProcessor , which is defined as:
    Code: C / C++
    Log in, to see the code
    i.e., e.g. function String MyFunction(const String& param) . However, in your code you specify a function String WebServer::processor(const String& var) - these are two different types in C++.
    The function String F(const String&) is not the same as String Object::F(const String&) . You can work around this problem by declaring the function processor () in class WebServer as static:
    Code: C / C++
    Log in, to see the code
    that is, one that is not called on behalf of a specific object.

    Added after 42 [minutes]: .

    The second and third errors are of the same type. You should use send () instead of send_P ():
    Code: C / C++
    Log in, to see the code
    .
    The function send_P () could only be used in this context if the third argument were of type PGM_P . Declarations of both functions:
    Code: C / C++
    Log in, to see the code
    .
    https://github.com/me-no-dev/ESPAsyncWebServer/blob/master/src/ESPAsyncWebServer.h
  • #3 18827162
    sq9etc
    Level 12  
    khoam wrote:
    The second and third errors are of the same type. You should use send() instead of send_P():
    .
    Then why does it work send_P() in the non-object version?
  • ADVERTISEMENT
  • #4 18827327
    khoam
    Level 42  
    The answer is in the error message:
    Code: Bash
    Log in, to see the code
    Function send_P () is called in the context of this (given) object, so there is a problem with passing the default value of the 4th argument, which should be of function type AwsTemplateProcessor (see my previous post).
    It is better to use a version of the function send () as I wrote above.
  • #5 18828565
    sq9etc
    Level 12  
    For the first error I converted the function processor to static according to your suggestion. Now I have 2 errors that it does not know t and h .
    For the other two errors I changed the function calls send_P() to send() , but I still get the error 'this' was not captured for this lambda function .
  • #6 18828578
    khoam
    Level 42  
    sq9etc wrote:
    For the other two errors I changed the send_P() function calls to send(), but I still get the error 'this' was not captured for this lambda function.
    .
    Please show just this code snippet after 'refactoring' to send () and error logs.
  • ADVERTISEMENT
  • #7 18828591
    sq9etc
    Level 12  
    Code: C / C++
    Log in, to see the code
    .

    Code: Bash
    Log in, to see the code
    .
  • Helpful post
    #8 18828637
    khoam
    Level 42  
    The variables h and t belong to object of class WebServer , so they must be "captured" by the lambda function in server.on (). Try this code after modification:
    Code: C / C++
    Log in, to see the code
    It is [&] .
  • ADVERTISEMENT
  • #9 18828650
    sq9etc
    Level 12  
    Thanks, that helped.
    I still don't know what to do to make the static function processor see the fields t and h .
    Code: Bash
    Log in, to see the code
    .
  • #10 18828669
    khoam
    Level 42  
    sq9etc wrote:
    I still haven't figured out what to do to make a static processor function see the t and h fields.
    .
    Unfortunately, a static function of a class can only see static variables of that class, unless you additionally provide (as an argument) a reference to an object of that class. In this case, this is quite difficult to do. Try making processor () a normal function again (remove the specifier static ) along with modifying the code snippet:
    Code: C / C++
    Log in, to see the code
    Probably an error will pop up, but something further can be done about it.
  • #11 18837265
    sq9etc
    Level 12  
    I did as recommended. Now there is this error:
    Code: Bash
    Log in, to see the code
    .

    i.e. again can't match method String WebServer::processor(const String& var) as parameter of function send_P .
  • Helpful post
    #12 18837431
    khoam
    Level 42  
    You need to add a local helper function procfun ():
    Code: C / C++
    Log in, to see the code
    Function processor () is to remain non-static.
  • #13 18838169
    sq9etc
    Level 12  
    Thanks, you are great. Now it compiles and even works:) .

Topic summary

The discussion revolves around issues faced when attempting to refactor an Arduino project using the ESPAsyncWebServer library by moving functions into their own classes. The user initially encounters compilation errors related to function type mismatches and variable scope, particularly with the static function `processor` not being able to access instance variables `t` and `h`. Solutions proposed include making the `processor` function non-static and using lambda functions to capture the necessary variables. Ultimately, the user successfully resolves the issues and achieves a working implementation.
Summary generated by the language model.
ADVERTISEMENT