3

I would like to create a personal library where I use another one. In my code I declared and initialized the library in the private part.

But I have the error '((LCD*)this)->LCD::lcd' does not have class type.

I've written several versions, but nothing changes. At best I can display the print Test01 and test02.

.h

#ifndef LCD_h
#define LCD_h

#include <LiquidCrystal_I2C.h>

class LCD{
  public:
    LCD();
    void firstLine();
    void secondLine(float tempInCelsius);

  private:
    LiquidCrystal_I2C lcd(0x27, 16, 2);
};

#endif

.cpp

#include "LCD.h"
#include <LiquidCrystal_I2C.h>

LCD::LCD(){
    Serial.begin(9600);
    Serial.println("Test 01");
    lcd.init();
    lcd.backlight();
    lcd.setCursor(0, 0);
}

void LCD::secondLine(float tempInCelsius){
    Serial.println("Test 03");
    lcd.clear();
    lcd.setCursor(0, 1);
    lcd.print("T = ");
    lcd.print(tempInCelsius);
}

.ino

#include "LCD.h"


LCD CrystalLCD();

void setup(void)
{
    Serial.begin(9600);
    Serial.println("Test 02");
}

void loop(void)
{
    CrystalLCD.secondLine(1.40);
}

I'll give you the entire error message, too.

[Starting] Verify sketch - arduino.ino
Loading configuration...
Initializing packages...
Preparing boards...
Verifying...
In file included from /Users/WorkSpace/Make/RucheChaufante/ino/LCD.cpp:1:0:
LCD.h:13: error: expected identifier before numeric constant
     LiquidCrystal_I2C lcd(0x27, 16, 2);
                           ^
LCD.h:13: error: expected ',' or '...' before numeric constant
/Users/WorkSpace/Make/RucheChaufante/ino/arduino.ino: In constructor 'LCD::LCD()':
arduino:9: error: '((LCD*)this)->LCD::lcd' does not have class type
     Serial.println("Test 02");
     ^
arduino:10: error: '((LCD*)this)->LCD::lcd' does not have class type
 }
     ^
arduino:11: error: '((LCD*)this)->LCD::lcd' does not have class type

     ^
/Users/WorkSpace/Make/RucheChaufante/ino/arduino.ino: In member function 'void LCD::secondLine(float)':
arduino:16: error: '((LCD*)this)->LCD::lcd' does not have class type
arduino:17: error: '((LCD*)this)->LCD::lcd' does not have class type
arduino:18: error: '((LCD*)this)->LCD::lcd' does not have class type
arduino:19: error: '((LCD*)this)->LCD::lcd' does not have class type
/Users/WorkSpace/Make/RucheChaufante/ino/arduino.ino: In function 'void loop()':
arduino:14: error: request for member 'secondLine' in 'CrystalLCD', which is of non-class type 'LCD()'
     CrystalLCD.secondLine(1.40);
                ^
exit status 1
[Error] Exit with code=1
2
  • It would aid your question if you'd actually include the code that produced the error in context. Just supplying the single offending line often has the effect of hiding the real problem, which may exist on another line and yet manifest in an error of this line. Commented Jun 27, 2018 at 1:21
  • Thank you for your remark. I just added the entire error message. Commented Jun 27, 2018 at 6:15

1 Answer 1

3

You cannot initialize members in the class declaration. Try with:

class LCD
{   
  public:
    LCD();
    void firstLine();
    void secondLine(float tempInCelsius);

  private:
    LiquidCrystal_I2C lcd; 
};

But you can initialize such members (must, if they do not provide a default constructor) in the constructor(s). Try with:

LCD::LCD() : lcd(0x27, 16, 2) {
    Serial.begin(9600);
    Serial.println("Test 01");
    lcd.init();
    lcd.backlight();
    lcd.setCursor(0, 0);
}
Sign up to request clarification or add additional context in comments.

1 Comment

Another remark for futur reader. In the constructor should just set up variable values and instantiate objects and that is all. :) All my code in the consturctor is moved into an init() methode. Otherwise the code compiles but does not execute in Arduino.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.