expected identifier before numeric constant

Just learning - stuck on this. Code works fine in-line, but when try to encapsulate in class it fails on the instantiation of "display".

#include "SSD1306Wire.h"
class oled {
  private:
  SSD1306Wire  display(0x3c, 5, 4);

 public:
  oled(){
     display.init();
     display.flipScreenVertically();
  }
  void writeAt(int lineNumber, char* msg) {
    display.clear();
    display.setTextAlignment(TEXT_ALIGN_LEFT);
    display.drawString(0, (lineNumber - 1) * 16, msg);
    display.display();
  }
};

Please post your whole program and the complete error message

UKHeliBob: Please post your whole program and the complete error message

Here's the sketch. It fails at compile even with the main sketch commented out.

oled.cpp:4: error: expected identifier before numeric constant ** SSD1306Wire display(0x3c, 5, 4);** ** ^**

#include "oled.cpp"
oled screen;

void setup() {
  screen.writeAt(2, "message");

void loop() {
}

Try:

#include "SSD1306Wire.h"
class oled {
  private:
    SSD1306Wire display;

  public:
    oled() : display(0x3c, 5, 4) {
      display.init();
      display.flipScreenVertically();
    }
    void writeAt(int lineNumber, char* msg) {
      display.clear();
      display.setTextAlignment(TEXT_ALIGN_LEFT);
      display.drawString(0, (lineNumber - 1) * 16, msg);
      display.display();
    }
};

Also, you wouldn’t normally #include a ‘.cpp’ file. Class declarations go in a ‘.h’ file that you #include in files where they are used. Class definitions go in a ‘.cpp’ file that does not get #include(d).

Thanks! So much to learn.... Yes, I originally was using headers but did this as part of trying to debug. Off to read about initializers.

Here's the sketch

If that really is the whole sketch then where does the setup() function end ?

Sorry, that missing curly brace was a cut and paste error. Really appreciate your help (I worked on this for hours).

nomlas: Sorry, that missing semicolon was a cut and paste error. Really appreciate your help (I worked on this for hours).

Which semicolon was that ? I was referring to the missing } at the end of the setup() function

Please post a complete sketch that illustrates the problem

You are navigating into trouble. Your class "oled" is initializing the "display" in a constructor which is invoked before the "setup()" method of the sketch is reached. This could cause the "display" to get initialized before the hardware is ready for it. Usually you would create a "MyBeautifulClass::begin()" method from where you initialize anything hardware related and call that method from "setup()".

EDIT: Oh, and in C++ you either create your entire class in the header (*.h) file or you create the prototype in the header and the implementation in a *.cpp file. The latter is the usual approach.

Thank you both so much. I must say that this is the first question I've ever posted on any forum (believe it or not), and I wasn't expecting such quick and helpful responses.

I meant } and tried to edit the response. Sorry you saw it first.

I've read about using a begin method rather than doing initialization in the constructor and that probably explains some other strange results. Thanks for the reminder. I will change to a standard header/class arrangement now as well.