SOLVED: conversion from '__FlashStringHelper*' to non-scalar type 'String'

Hi All

I am having trouble using strings and F() and PROGMEM etc. Probably from a shortage of knowledge...

The call to debugWrite below gives me the following error
error: conversion from '__FlashStringHelper*' to non-scalar type 'String' requested

void someMethod() {
  ...doStuff...
  debugWrite(F("BotTesting v1.0 29/04/2012"));
  ..doMoreStuff
}

void debugWrite(String data) {
  if ( debugWriteFlag == 1 || debugWriteFlag == 3 ) {
    lcd.print(data);
  }
  if ( debugWriteFlag == 2 || debugWriteFlag == 3 ) {
    Serial.println(data);
  }
}

Please help. I wish to have flexibility in whether I write to Serial or my onboard LCD display.

Thanks

F() only works for functions that take a PROGMEM pointer of type __FlashStringHelper. Serial.print() is such a function. Apparently debugWrite() isn't.

Hi John.

I was hoping to get an answer as what to actually do to sort out my problem, but after some searching I found example code suggesting I write an overloaded procedure for debugWrite. So now I have debugWrite defined twice, with different parameter types.

void debugWrite(String data) {
  if ( debugWriteFlag == 1 || debugWriteFlag == 3 ) {
    lcd.print(data);
  }
  if ( debugWriteFlag == 2 || debugWriteFlag == 3 ) {
    Serial.println(data);
  }
}

void debugWrite( __FlashStringHelper* data) {
  if ( debugWriteFlag == 1 || debugWriteFlag == 3 ) {
    lcd.print(data);
  }
  if ( debugWriteFlag == 2 || debugWriteFlag == 3 ) {
    Serial.println(data);
  }
}

So both the following 2 calls will work

debugWrite(F("PROGMEM string");
debugWrite("another RAM string");

For anyone that would like to give a helpful explanation for a dabbler, I found the code would not compile until I placed the asterisk at the end of the __FlashStringHelper type declaration as in __FlashStringHelper*

The explanation is simple:

Here is the definition of the macro F():
#define F(string_literal) (reinterpret_cast<__FlashStringHelper*>(PSTR(string_literal)))

This macro gives you a pointer to the class __FlashStringHelper, so that is what your code needs to accept as a formal parameter.