Pages: [1]   Go Down
Author Topic: SOLVED: conversion from '__FlashStringHelper*' to non-scalar type 'String'  (Read 1747 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Full Member
***
Karma: 0
Posts: 111
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
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
« Last Edit: May 07, 2012, 09:13:47 am by AgeingHippy » Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 201
Posts: 8689
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Offline Offline
Full Member
***
Karma: 0
Posts: 111
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

Code:
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
Code:
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*
Logged

California
Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Pages: [1]   Go Up
Jump to: