Hello,
I wrote a function and thought I would attempt to put it inside a library (MyLib) to use it inside another sketch. I did the following:
- Created a MyLib.h file
- Created a MyLib.cpp file
- Created a keywords.txt file
Then, I tried to use that function through the library inside a new sketch.
At the top, I put:
#include <MyLib.h>
MyLib mylib;
When attempting to compile, I get the following error:
Arduino\libraries\MyLib/MyLib.h:6:1: error: 'Class' does not name a type
Class MyLib
I have tried many things but I just can't figure it out.
This is the code for the original function before being put inside the library:
char* IntToFmt(int num, int digit, char delim) {
int nDigits = floor(log10(abs(num))) + 1;
char *numchr;
numchr = (char *) malloc(sizeof(char) * (nDigits+1));
char *fmtchr;
int numlen;
int delimnum;
int fmtnumlen;
int adj = 0;
double ratio;
itoa(num, numchr, 10);
numlen = strlen(numchr);
if (num < 0 && (numlen-1) % digit == 0) {
adj = 1;
}
ratio = ceil(1.000 * (numlen-adj) / digit);
delimnum = ratio - 1;
fmtnumlen = numlen + delimnum;
fmtchr = (char *) malloc(sizeof(char) * (fmtnumlen+1));
int a = 0;
int b = fmtnumlen - 1;
for (int c = 0; c < (fmtnumlen); c++) {
//Serial.println((c + 1) % (digit + 1));
if (c!=0 && (b + 1 ) % (digit + 1) == 0) {
fmtchr[c] = delim;
}
else {
fmtchr[c] = numchr[a];
a++;
}
b--;
}
fmtchr[fmtnumlen] = '\0';
return fmtchr;
free(fmtchr);
free(numchr);
}
void setup() {
// put your setup code here, to run once:
Serial.begin(74880);
int blcksiz = 3;
char delimchar = ',';
int a = 123456;
int b = 1234567;
int c = 12345678;
int d = 123456789;
int w = -123456;
int x = -1234567;
int y = -12345678;
int z = -123456789;
Serial.println("");
Serial.println("The formatted result of a is : " + String(IntToFmt(a, blcksiz, delimchar)));
Serial.println("The formatted result of a is : " + String(IntToFmt(b, blcksiz, delimchar)));
Serial.println("The formatted result of a is : " + String(IntToFmt(c, blcksiz, delimchar)));
Serial.println("The formatted result of a is : " + String(IntToFmt(d, blcksiz, delimchar)));
Serial.println("");
Serial.println("The formatted result of a is : " + String(IntToFmt(w, blcksiz, delimchar)));
Serial.println("The formatted result of a is : " + String(IntToFmt(x, blcksiz, delimchar)));
Serial.println("The formatted result of a is : " + String(IntToFmt(y, blcksiz, delimchar)));
Serial.println("The formatted result of a is : " + String(IntToFmt(z, blcksiz, delimchar)));
}
void loop() {
// put your main code here, to run repeatedly:
}
This is the content of the .h file:
#ifndef MyLib_h
#define MyLib_h
#include "Arduino.h"
Class MyLib
{
public:
MyLib();
char* IntToFmt(int num, int digit, char delim);
private:
int _num;
int _digit;
char _delim;
int _nDigits;
char *_numchr;
char *_fmtchr;
int _numlen;
int _delimnum;
int _fmtnumlen;
int _adj;
double _ratio;
int _a;
int _b;
int _c;
};
#endif
This is the content of the .cpp file:
#include <Arduino.h>
#include <MyLib.h>
MyLib::MyLib()
{
}
char* MyLib::IntToFmt(int num, int digit, char delim) {
_num = num;
_digit = digit;
_delim = delim;
_nDigits = floor(log10(abs(_num))) + 1;
_numchr = (char *) malloc(sizeof(char) * (_nDigits + 1));
_adj = 0;
itoa(_num, _numchr, 10);
_numlen = strlen(_numchr);
if (_num < 0 && (_numlen - 1) % _digit == 0) {
_adj = 1;
}
_ratio = ceil(1.000 * (_numlen - _adj) / _digit);
_delimnum = _ratio - 1;
_fmtnumlen = _numlen + _delimnum;
_fmtchr = (char *) malloc(sizeof(char) * (_fmtnumlen + 1));
_a = 0;
_b = _fmtnumlen - 1;
for (int _c = 0; _c < (_fmtnumlen); _c++) {
if (_c != 0 && (_b + 1 ) % (_digit + 1) == 0) {
_fmtchr[_c] = _delim;
}
else {
_fmtchr[_c] = _numchr[_a];
_a++;
}
_b--;
}
_fmtchr[_fmtnumlen] = '\0';
return _fmtchr;
free(_fmtchr);
free(_numchr);
}
This is the content of the .txt file:
MyLib KEYWORD1
IntToFmt KEYWORD2
The 3 files are in the Arduino/libraries/MyLib folder.
If anybody could help to find where I messed-up it would be great.
Thanks.