0

This question already has an answer here:

I am struggling to implement a custom class that uses the Arduino Extended Database Library (EDB.h). The purpose of my custom class is to manage my application's internal database.

While searching for a solution I found this, which seems very relevant: the code and goal are the same as mine.

But my problem with that Q&A is that I am not able to implement the given solution as I don't fully understand the way it should be written.

For the sake of trying to give the best support for explanation, I upladed the code below. Feel free to explain me how to modify it.

Thank you.

EDIT: Following duplicate comments, I'll try to be more precise in my question.

marked as duplicate by Juraj, VE7JRO, gre_gor, MatsK, Code Gorilla Apr 12 '18 at 10:09

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • Indeed, I even posted the link. If I did it's for a specific reason: I do not understand the solution. May be you could help ? Thx Juraj – Thibault Apr 10 '18 at 9:56
  • I think it is explained good there – Juraj Apr 10 '18 at 10:38
  • Juraj, sure it must be good if one have the knowledge, which I haven't. In clear: do you understand that I do not understand the given explanation ? If its good enough for you, and you understand it, may be you could please give me more details on how to implement it ? Thanks – Thibault Apr 10 '18 at 11:02
  • simplified: you can't give a class member function, where function is required. – Juraj Apr 10 '18 at 11:40
  • @Juraj That's not entirely correct. – Code Gorilla Apr 10 '18 at 11:43
1

It is difficult to answer this question because I'm not sure what you have managed to achieve. Sorry if some of it seems a bit basic.

Look at Majenko's answer to the question. If you want to use this EDB class then you need to define two functions:

  void configWriter( unsigned long address, byte data );
  byte configReader( unsigned long address );

You need to be able to access the single instance of these functions from anywhere in the code, so the need to have global scope. When they are global you can assign them to pointers by using the function name and no brackets/parenthesis. i.e.

aPtr = configWriter;

So the call to create the EDB instance would be:

 configDB = new EDB( configWriter, configReader ); 

Now you could create the EDB instance using a class member function. To do this you have to make the function a static member, which means it has no access to the class's data. (in its simplest terms static means there is only one of the things it is referring to) i.e.

class myEDB
{
  public:
    static void configWriter( unsigned long address, byte data );
    static byte configReader( unsigned long address );
...
};
configDB = new EDB( myEDB::configWriter, myEDB::configReader ); 

Essentially its the same thing, but the code is compartmentalised better and you end up with neater code.

However, if you are only ever going to have one instance of the myEDB class then you can have a static member variable that hold the value of this. You set this value in the class constructor.

class myEDB
{
  public:
    static void configWriter( unsigned long address, byte data );
    static byte configReader( unsigned long address );
    myEDB() {pInstance = this;}
  private:
    static myEDB* pInstance = nullptr;
...
};
configDB = new EDB( myEDB::configWriter, myEDB::configReader ); 

When configReader or configWriter is called it checks to see if the member pInstance has a non null value, if it doesn't then it creates a new instance. The then use this pInstance to access the functions and variables for the instance of the class. (This is the design pattern for a single instance class or singleton).

SO what should YOU do?

Start simply, define two global functions that do what you need as per the top of this answer. If you get stuck, if you need more info feel free to ask or comment.

  • Hello and Thank you again Code Gorilla. I posted my code following your explanation, and the resulting compilation errors. Can you help me to understand these errors, and modify the code accordingly ? – Thibault Apr 12 '18 at 5:55

Not the answer you're looking for? Browse other questions tagged or ask your own question.