New to classes, Error: request for member '-' in '1'

Hi,
I've been playing with arduino projects for a while, but I'm new to using classes. This is the first project where I have decided to dive into them.

I'm having issues using U8G2 inside a class (drawStr() causes crash and reboot when u8g2 is inside class · Issue #858 · olikraus/u8g2 · GitHub), so I've decided to try and make a simple example to show the problem for others on github.

However my simple example does not even compile, and returns the error code:

exit status 1
request for member 'initialize' in '1', which is of non-class type 'int'

I've set up classes in a similar fashion in my actual project, and everything compiles perfectly. I've used separate files because that's how its done in the actual project (there will end up being far too much for a single file).

Why does it think the "initialize" function is a member of '1'? It is a member of DISPLAY which is an instance of DISPLAY_class. Does it think of my whole class as an int? Am I declaring the instance of my class incorrectly?

Thanks

u8g2_CrashandBoot.zip (1.98 KB)

Don't you think more people would look at your code if you posted it as text (in code braces of course) rather than having to download it?

Please post the complete error message

Metallor:
Don't you think more people would look at your code if you posted it as text (in code braces of course) rather than having to download it?

Especially a sketchy .zip file.

Sorry guys here is actual code.

Main sketch:

#include <U8g2lib.h>
#include <SPI.h>
#include <Wire.h>
#include "DISPLAY.h"

U8G2_ST7920_128X64_F_HW_SPI u8g2(U8G2_R0, LCD_PIN_CS, LCD_PIN_RESET);
DISPLAY_class DISPLAY(5, u8g2);

void setup() {
  Serial.begin(115200);
  Serial.println("");
  Serial.println("");
  Serial.println("ARDUINO BOOTING...");
  u8g2.begin();
  Wire.begin();
  Wire.setClock(400000);
  DISPLAY.initialize();
  DISPLAY.drawHelloWorld();
}

void loop() {
  delay(1000);
  DISPLAY.drawNumber();
  DISPLAY.increaseNumber();
}

Seperate DISPLAY.h file in same directory:

#ifndef DC_H
#define DC_H

#define LCD_PIN_BACKLIGHT 12  //Pin to turn backlight on
#define LCD_PIN_CS 53         //used in u8g2 constructor
#define LCD_PIN_RESET 50      //used in u8g2 constructor

#include <Arduino.h>
#include <U8g2lib.h>
#include <SPI.h>
#include <Wire.h>

class DISPLAY_class {
  public:
    DISPLAY_class(int number, U8G2& U8G2_ref);
    void initialize();
    void drawHelloWorld();
    void drawNumber();
    void increaseNumber();
  private:
    U8G2* u8g2;
    int _number;
};

#endif

Seperate DISPLAY.cpp file in same directory:

#include <Arduino.h>
#include <U8g2lib.h>
#include <SPI.h>
#include <Wire.h>
#include "DISPLAY.h"

DISPLAY_class::DISPLAY_class(int number, U8G2& U8G2_ref) {
  _number = number;
  u8g2 = &U8G2_ref;
}
void DISPLAY_class::initialize() {
  digitalWrite(LCD_PIN_BACKLIGHT, HIGH); //turn on LCD backlight
  //do other stuff
  Serial.println("Display Class Initialised!");;
};
void DISPLAY_class::drawHelloWorld() {
  Serial.print("1. CLEAR BUFFER(): ");
  delay(1000); //delay so debug message can be passed via Serial
  u8g2->clearBuffer();          // clear the internal memory
  Serial.println("OK!");
  Serial.print("2. SET FONT(): ");
  delay(1000);
  u8g2->setFont(u8g2_font_ncenB08_tr); // choose a suitable font
  Serial.println("OK!");
  Serial.print("3. DRAW STRING(): ");
  delay(1000);
  u8g2->drawStr(0, 10, "Hello World!"); // write something to the internal memory
  Serial.println("OK!");
  Serial.print("4. SEND BUFFER(): ");
  delay(1000);
  u8g2->sendBuffer();          // transfer internal memory to the display
};
void DISPLAY_class::drawNumber() {
  u8g2->clearBuffer();
  u8g2->setFont(u8g2_font_ncenB08_tr); // choose a suitable font
  u8g2->drawStr(0, 10, _number); // write something to the internal memory
  u8g2->sendBuffer();
}
void DISPLAY_class::increaseNumber(){
  _number++;
}

And the error messages I receive are:

In file included from C:\Temp\arduino_build_550774\sketch\u8g2_CrashandBoot.ino.cpp:1:0:

C:\Users\Daniel\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.6.21\cores\arduino/Arduino.h:55:17: error: expected unqualified-id before numeric constant

 #define DISPLAY 0x1

                 ^

C:\Users\Daniel\Desktop\u8g2_CrashandBoot\u8g2_CrashandBoot.ino:7:15: note: in expansion of macro 'DISPLAY'

 DISPLAY_class DISPLAY(5, u8g2);

               ^

C:\Users\Daniel\Desktop\u8g2_CrashandBoot\u8g2_CrashandBoot.ino: In function 'void setup()':

u8g2_CrashandBoot:17: error: request for member 'initialize' in '1', which is of non-class type 'int'

   DISPLAY.initialize();

           ^

u8g2_CrashandBoot:18: error: request for member 'drawHelloWorld' in '1', which is of non-class type 'int'

   DISPLAY.drawHelloWorld();

           ^

C:\Users\Daniel\Desktop\u8g2_CrashandBoot\u8g2_CrashandBoot.ino: In function 'void loop()':

u8g2_CrashandBoot:23: error: request for member 'drawNumber' in '1', which is of non-class type 'int'

   DISPLAY.drawNumber();

           ^

u8g2_CrashandBoot:24: error: request for member 'increaseNumber' in '1', which is of non-class type 'int'

   DISPLAY.increaseNumber();

           ^

Using library U8g2 at version 2.22.18 in folder: C:\Users\Daniel\Documents\Arduino\libraries\U8g2 
Using library SPI at version 1.0 in folder: C:\Users\Daniel\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.6.21\libraries\SPI 
Using library Wire at version 1.0 in folder: C:\Users\Daniel\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.6.21\libraries\Wire 
exit status 1
request for member 'initialize' in '1', which is of non-class type 'int'

Don't call your object DISPLAY.

Thanks. Now you point it out that makes sense (DISPLAY has already been defined as 0x1 in a macro somewhere).

Would that #define be in one of the library's I'm using? Or is it an Arduino thing?

Is there a list of names I shouldn't use somewhere for future reference?

The error message tells you that it was #define'd in Arduino.h

No there's no easy way of seeing everything that's defined. But if you stick to a consistent naming scheme, you usually stay out of trouble:

  • Constants #define'd are UPPERCASE
  • Class names are TitleCase
  • Functions, variables and instances of classes are camelCase
  • Private variables inside classes are _underscorePrefixed

Examples...

  • DISPLAY, BIT()
  • Serial, AccelStepper
  • digitalRead(), print(), myDisplay
  • _mySerial

Sometimes, for readability, the UPPERCASE #define's use underscores, like UPPER_CASE. You would not usually use underscore in variable or function names because the capitalization makes it readable.

The most complete solution is to use namespaces. This is a C++ feature not available in plain old C. The AccelStepper library is one of the best Arduino examples of the consistent use of namespaces.