Deca
Deca

Reputation: 153

Error header C++ when create an obj

I have a problem when I create an obj in my header file. I can't insert value in the constructor. (Error: expected a type specifier). I tried adding constants, but it didn't work. Why? How can I do? Thanks!

lcd.h

#ifndef __LCD__
#define __LCD__

#include "Device.h"
#include "Arduino.h"
#include <LiquidCrystal_I2C.h>

class Lcd: public Device {

  public:
    Lcd();

    void switchOn();
    void switchOff();
    void setFirstRow(String str);
    void setSecondRow(String str);

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

};

endif

Lcd.cpp

#include "Lcd.h"
#include "Arduino.h"

Lcd::Lcd(){
  lcd.init();
}

void Lcd::switchOn(){
  lcd.backlight();
}

void Lcd::switchOff(){
  lcd.clear();
  lcd.noBacklight();
}

void Lcd::setFirstRow(String str){
  lcd.setCursor(0,0);
  lcd.print(str);
}

void Lcd::setSecondRow(String str){
  lcd.setCursor(0,1);
  lcd.print(str);
}

Upvotes: 1

Views: 57

Answers (2)

amchacon
amchacon

Reputation: 1961

Let´s this declaration:

LiquidCrystal_I2C lcd;

And you can use this sintax in constructor

Lcd::Lcd() : lcd(0x27,16,2){
  lcd.init();
}

Upvotes: 2

R Sahu
R Sahu

Reputation: 206577

If you are using C++11, you should be able to use:

  LiquidCrystal_I2C lcd = LiquidCrystal_I2C(0x27, 16, 2);

If you are using earlier versions of C++, you need to put the initialization code in the definition of the constructor.

Upvotes: 2

Related Questions