Send String to a function inside a class I made a little class for handling menus. When creating a new instance, I pass a reference to the oled object made in main.cpp. Yet when I want to render a string using a variable, it throws an error. But I can pass a string directly to the render function with no problems. See comments in the code for what is working and what is not.


#include "Arduino.h"
#include "U8glib.h"
#pragma once

class Menu {
  U8GLIB_SSD1306_128X64&amp; oled;

  public:
    Menu(U8GLIB_SSD1306_128X64&amp; oled): oled(oled) {
      this-&gt;oled = oled;
    };
    void render() {
      this-&gt;oled.firstPage();
      do {
        String text = "test2";
        this-&gt;oled.setFont(u8g_font_unifont);
        this-&gt;oled.setPrintPos(0, 20);
        this-&gt;oled.drawStr(text); // not working
        this-&gt;oled.drawStr(text.c_str()); // not working
        this-&gt;oled.drawStr("works"); // works
      } while (this-&gt;oled.nextPage());
    }
  };


In the case of this-&gt;oled.drawStr(text); the error is:

error: no matching function for call to 'U8GLIB_SSD1306_128X64::drawStr(String&amp;)


In case of this-&gt;oled.drawStr(text.c_str()) the error is:

error: no matching function for call to 'U8GLIB_SSD1306_128X64::drawStr(const char*)


How I create the instance in main.cpp:

U8GLIB_SSD1306_128X64 oled(U8G_I2C_OPT_NONE);
Menu menu(oled);
menu.render();
Try with the new version, U8g2lib.

With that version, you can write code like this:

String t1 = "Grow";
String t2 = "Room";
u8g2.firstPage();
do {
  u8g2.setFont(u8g2_font_logisoso24_tf);
  u8g2.drawStr(32, 24, t1.c_str());
  u8g2.drawStr(32, 60, t2.c_str());
} while(u8g2.nextPage());