Float to String Arduino Compile Error

I'm using this library to convert a float to a string: http://www.arduino.cc/playground/Main/FloatToString?action=sourceblock&ref=1 .

This is the snippet of code, where printing out flt looks like "29.37":

    float flt = tempSensor.getTemperature();
    char buffer[25];
    char str[20];
    Serial.print(floatToString(str, flt, 2, 10));

This should work out of the box, but doesn't - what did I do wring? These are my compile errors:

.../floatToString.h:11: error: expected primary-expression before ',' token
.../floatToString.h: In function 'char* floatToString(char*, float, int, int, bool)':
.../floatToString.h:11: error: default argument missing for parameter 5 of 'char* floatToString(char*, float, int, int, bool)'
.../floatToString.h:73: error: 'itoa' was not declared in this scope
.../floatToString.h:89: error: 'itoa' was not declared in this scope

Answers


In C++ only all last parameters are allowed to have a default value:

BAD rightjustify MUST have a default value:

char * floatToString(char * outstr, float value, int places,
    int minwidth=0, bool rightjustify) {

OK: no default values, last or two last parameters have default values

char * floatToString(char * outstr, float value, int places,
    int minwidth, bool rightjustify) {

char * floatToString(char * outstr, float value, int places,
    int minwidth, bool rightjustify=false) {

char * floatToString(char * outstr, float value, int places,
    int minwidth=0, bool rightjustify=false) {

Check your header, I guess the one you linked is not the one you currently use.

There is another pointer to an issue: ito is unknown to the compiler. It should be in cstdlib, so an #include <cstdlib> is missing, I would put it in the header, because it depends on it.


default argument missing for parameter 5 of 'char* floatToString(char*, float, int, int, bool)

Looks like you're missing a value: floatToString(str, flt, 2, 10)

Try adding True or False to the end there


I had the same error message; it turns out I had #included "floatToString.h" twice, once in my .ino file and once in one of the classes I used. I removed one of them, and then the (rather misguiding) error message disappeared!


Need Your Help

How to printf "unsigned long" in C?

c printf long-integer unsigned format-specifiers

I can never understand how to print unsigned long datatype in C.

Objective C NSMutableString* property retain count oddity

iphone objective-c nsmutablestring

if I create an nsmutablestring and then release it , shouldn't the retain count be 0?