I am trying to build my first library and I am having some trouble with a function which uses char pointer.
Normally in my sketch I had this function:
// t is time in seconds = millis()/1000;
char * TimeToString(unsigned long t)
{
static char str[12];
// t = currentMillis/1000;
t /= 1000;
long h = t / 3600;
t = t % 3600;
int m = t / 60;
int s = t % 60;
sprintf(str, "%04ld:%02d:%02d", h, m, s);
return str;
}
Which I call like this...
Serial.println(TimeToString(millis()));
I am trying this but it is not working...
Inside .h
// 3) Name de Class
class FunctionsClass
{
// 4) Define the public and private methods
public:
char TimeToString();
}
Inside .cpp
// t is time in seconds = millis()/1000;
char FunctionsClass::*TimeToString(unsigned long t)
{
static char str[12];
// t = currentMillis/1000;
t /= 1000;
long h = t / 3600;
t = t % 3600;
int m = t / 60;
int s = t % 60;
sprintf(str, "%04ld:%02d:%02d", h, m, s);
return str;
}
I am getting...
C:.....\Documents\Arduino\libraries\SergeFunctions\SergeFunctions.cpp: In function 'char FunctionsClass::* TimeToString(long unsigned int)':
C:......\Arduino\libraries\SergeFunctions\SergeFunctions.cpp:68:10: error: cannot convert 'char*' to 'char FunctionsClass::*' in return
return str;
^
C:......\Arduino\libraries\SergeFunctions\SergeFunctions.cpp:69:1: warning: control reaches end of non-void function [-Wreturn-type]
}
^
Error compiling.
Any help on getting it right, I would also like to understand why this is happening.
char FunctionsClass::*TimeToString(unsigned long t)
is NOT how to implement the function declared in the header file. The * is in the wrong place (actually shouldn't even be there according to the prototype).
The method name is FunctionsClass::TimeToString. Nothing goes anywhere in the string. Before, yes. After, yes. Between, no.
Pick a library used by the Arduino IDE. Most have a header file associated with them that declares what the class looks like. For example:
#ifndef FunctionClass_h // If this is the first time we've read this...
#define FunctionClass_h // define this so we don't "double include" it.
class FunctionClass {
public:
char *TimeToString(unsigned long t);
}; // Note semicolon
#endif // Note that this closes what can be a long #ifndef
The *.cpp file, which is tied to the header file, is then used to define the methods that are used in the class. For example:
#include <stdio.h>
#include "FunctionClass.h"
char *FunctionClass::TimeToString(unsigned long t)
{
static char str[12];
// t = currentMillis/1000;
t /= 1000;
long h = t / 3600;
t = t % 3600;
int m = t / 60;
int s = t % 60;
sprintf(str, "%04ld:%02d:%02d", h, m, s);
return str;
}
A way to verbalize this class method is: "TimeToString() is a method of the FunctionClass class and uses an unsigned long as its argument. The method returns a char pointer." As Paul pointed out, your method declaration in the header file does not match the method definition in the cpp file; you left out the method's argument. The compiler's not going to like that.
Other variations are also possible, but this format is pretty common in Arduino libraries. There are a lot of good tutorials on C++ out there. Take a look at a few and spend some time with one that makes sense to you.
I got it working, let me update with the result for anyone who might need it...
.h file
// 3) Name de Class
class FunctionsClass
{
// 4) Define the public and private methods
public:
char* TimeToString(unsigned long t);
private:
// int aPrivateFunction;
};// Note semicolon
.cpp file
// A way to verbalize this class method is: "TimeToString() is a method of the FunctionClass class and uses an unsigned long as its argument. The method returns a char pointer."
char* FunctionsClass::TimeToString(unsigned long t)
{
static char str[12];
t /= 1000;
long h = t / 3600;
t = t % 3600;
int m = t / 60;
int s = t % 60;
sprintf(str, "%04ld:%02d:%02d", h, m, s);
return str;
}