-1

Hello I am using the u8glib library to draw text on the string but it keeps giving me an error with the string

 String name1 = "";
 ...
 void draw(){
  u8g.setFont(u8g_font_micro);
  u8g.drawStr(22, 37, name1);
 }

why dosnt this work?

Here is the error:

C:\Program Files (x86)\Arduino\libraries\U8glib/U8glib.h:200:16: note: no known conversion for argument 3 from 'int' to 'const __FlashStringHelper*' no matching function for call to 'U8GLIB_PCD8544::drawStr(int, int, String&)'

0

2 Answers 2

2

String is evil and should be avoided at all costs (http://hacking.majenko.co.uk/the-evils-of-arduino-strings)

The author of u8glib knows that and has refused (quite rightly) to implement them.

You should not use String either. If you really must for some obscure reason then you will have to pass the internal "C string" reference to the drawStr function instead of the whole String object:

u8g.drawStr(22, 37, name1.c_str());

The only reason people use String is because they are too lazy to learn the proper way of handling textual data in C.

4
  • The link is broken: "Hmm. We’re having trouble finding that site. We can’t connect to the server at hacking.majenko.co.uk." Commented Feb 6, 2023 at 3:48
  • Re "String is evil": I have come to the same conclusion, after some pain. It was there all along, but it didn't really show up until now. It was used in formatting of error reporting going to the serial port and as the frequency of (spurious, harmless) errors increased due to some changes to the system, occasional hard freezes was the result. Strangely, it was dependent on the Serial Monitor (or another terminal, like Screen) being open/connected... It being open made the error nearly 100% reproducible. Commented Feb 6, 2023 at 3:55
  • cont' - That was with an Arduino Leonardo. Commented Feb 6, 2023 at 4:01
  • If they shouldn't use String, what should they use instead? Perhaps add something to this effect to the answer? And/or a reference to this answer. Commented Feb 6, 2023 at 4:02
0

Like the documentation says, that method doesn't take a String object. Call the c_str() method of the String object and pass that instead.

2
  • what do you mean, what should I put in my arduino code? I looked at the documentation and i cant see a c_str() method Commented Jan 17, 2016 at 9:44
  • name1.c_str()
    – Majenko
    Commented Jan 17, 2016 at 10:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.