I have a problem writing a library. I'd like to create one that usues the GetCSV.Value(pass variables here) and GetCSV.Count(pass variables here) functions and returns the data.
My sorry excuse for code is below.
// --------------------------------------------------------------------
// The intent of the libray is:
// String value = GetCSV.Value(String data, char separator, int index);
// int count = GetCSV.Count(String data, char separator);
// --------------------------------------------------------------------
// ----------------------------------------------------------------
//The GetCSV.h file
#ifndef GetCSV_h
#define GetCSV_h
#include "Arduino.h"
class GetCSV{
public:
String Value(String data, char separator, int index);
int Count(String data, char separator);
};
#endif
// End of GetCSV.h ------------------------------------------------
// ----------------------------------------------------------------
// The GetCSV.cpp file
String GetCSV::Value(String data, char separator, int index) {
// Do stuff here and return a String value
}
int GetCSV::Count(String data, char separator) {
// Do stuff here and return an integer value
}
// End of GetCSV.cpp -----------------------------------------------
// ----------------------------------------------------------------
// The Arduino Code
#include "GetCSV.h"
String x = "c,d,e,f";
void setup() {
String z = GetCSV.Value(x, ',' 0);
int y = GetCSV.Count(x, ',');
}
void loop() {}
But when I try to compile it, the IDE throws an error telling me "expected primary-expression before '.' token"
I'm likey missing something really stupid here. But there's little info out there regarding a class that uses a period and a name. If anyone can help here I would be forever grateful.
So how do I go about doing that with the code I wrote? I'd to learn more on how this is done but like I mentioned, there's little info out there regarding a class that uses a period and a name.
When I use Serial.print() I don't define an instance of Serial. Yet I can still call the underlying functions after the period. This is the question that's been giving me grief.
I could create a class that can be defined as an instance in the program but I would like the user (me) to use the name GetCSV without having it declared in the program. That's where the info gets harder to find online.
I hope that all made sense.
P.S. Have patience with me. I'm a 65 year old retired industrial electrian that loves spending time playing with microprocessors and trying to learn more.
The reason you can use Serial is because it's an already instantiated/declared HardwareSerial class. The instantiation/declaration happens in the core source files. You can do something similar in your library if you want.
All the documentation I could find did not cover using a library to return a value. Only to pass a value or values to the function to perform a task in of itself. It should have dawned on me that I needed to treat this as I would have using a function and assign a return type to it.
And this exercise has helped me to better understand the concepts of classes and instances of a class.
Thank you all for your help and understanding. I really do appreciate it.
If I remove "extern _GetCSV GetCSV;" from the .h file and "_GetCSV GetCSV;" from the .cpp file I would then need to create an instance of the class to use it? Sounds right to me anyway.
Thanks.
EDIT: I just tried it and found I needed to create the instance in the ino file to use it.
Question answered and marked as solved.
Sorry Power_Broker for the quick self answer to my question regarding removing the instance. I noticed you agreed just after my edit and before your post was deleted and I apprieciate the reply. Thanks so much for the help.