How to pass a function appUserFunc() to the commands[] array

There is a library file and the appUserFunc() user function. How to pass this function to a cell in the commands[] array.

#include "lib.h"

void appUserFunc(){ cout << "appUserFunc" << endl;}

int main()
{


    runTask();
}
#ifndef LIB_H
#define LIB_H

#include <iostream>
using namespace std;

void appSystemFunc(){ cout << "appSystemFunc" << endl;}


struct App
{   
    int num;
    string text;
    void (*f)(void);
};

App commands[]
{
    {1, "app system", appSystemFunc},
    {1, "app system", appUserFunc},
};

void runTask()
{
    for(App comm : commands)
    {
        comm.f();
    }
}

#endif

I have no clue about what you write, but almost all "passing" is done using pointers.

You are incorrectly partitioning what should go in a .h file and what should go in a .cpp file. See My Post #5 in this Thread.

That's possible. The executable file I have is cpp

The project is created on ESP32. Now I'm at the stage of creating a library for my project. The github link contains many files, please take a look.

I need to make the following mechanism. The kernel located in the library runs tasks, each task contains a function to call. It is necessary that the user can define his task from the file without changing the library file. I have given the simplest similar mechanism here.

I'm using Arduino methods. At this stage - in the example, I indicated pure C++.

I'm using a .ino file to run the entire system that is described in the library.

How can the code be not normal if both Arduino and the code in the github link are all C++. :grinning:

I answered you that I use Arduino methods to launch the entire project. See the files at the link.

In the example I indicated the simplest mechanism. The fact that there is no setup and loop here, well, excuse me.

Why am I not using the C++ language correctly?

I explained to you that this is a LIBRARY; a priori, Arduino’s main function cannot be here.

Arduino's main() function is typically hidden. It calls setup() and loop(). So fine. Instead of just posting a link to a GitHub with lots of code that has nothing to do with the code you showed in your first post .... please provide a short, complete example of what you're trying to do. Post it all inline here with code tags. Be sure to include both the .h and .cpp files for an example "library" along with a .ino file that uses that "library". The .ino should include setup() and loop(), but not main().

Again, short but complete.

The idea is that the people volunteering their time and effort here can, after glancing at the code, click a button to copy the .ino, and paste it directly into a new sketch in the Arduino IDE or at wokwi.com, along with another file or two in this case, and try it. Otherwise, we all waste energy (biological and electrical) trying to establish that common baseline. For example, if instead of that thing with main, you posted:

#include "lib.h"

void appUserFunc(){ cout << "appUserFunc" << endl;}

void setup() {
  Serial.begin(115200);
}

void loop() {
  runTask();
  delay(1500);
}

We could discuss

  • the forward declaration problem, which is why you posted in the first place? But also
  • that you should not define the global commands array in a header
  • if the use of cout is only incidental to the example, the header also should not have the
    #include <iostream>
    using namespace std;
    
  • "task" already has a meaning with ESP32, and you're already calling them "commands"
  • If you're doing other Arduino stuff, you might already be using String behind the scenes, so do you really need STL string as well?

So (unless you're trying for a header-only implementation) the library header would be more like

#pragma once

struct App
{   
    int num;
    String text;
    void (*f)(void);
};

extern App systemCommand;
void setupCommands(App commands[]);
void runCommands();

Does the list of commands need to be dynamic?

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.