Call function on lcd object in class

旧城冷巷雨未停 提交于 2021-01-28 22:47:12

问题


Currently i'm making a class for printing custom text to a LCD.

I pass the lcd object as paramter in the constructor to the class.

Display.h

#ifndef Display_h
#define Display_h

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

class Display
{
public:
  Display(LiquidCrystal_I2C *outsideLcd);

private:
 LiquidCrystal_I2C *lcd;
};

#endif

Display.cpp

#include "Arduino.h"
#include <Wire.h> 
#include <LiquidCrystal_I2C.h>
#include "Display.h"

Display::Display(LiquidCrystal_I2C *outsideLcd)
{
  lcd = outsideLcd;
  lcd.init();
  lcd.clear();
  lcd.print("Constructor");
}

Example.ino

#include "Display.h"
#include <Wire.h> 
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27,16,2);

Display display(&lcd);

void setup()
{

}

void loop()
{
}

When i call a function on the lcd object in the constructor keep getting these errors.

Display.cpp: In constructor 'Display::Display(LiquidCrystal_I2C*)':
Display.cpp:16: error: request for member 'init' in '((Display*)this)->Display::lcd', which is of non-class type 'LiquidCrystal_I2C*'
Display.cpp:17: error: request for member 'clear' in '((Display*)this)->Display::lcd', which is of non-class type 'LiquidCrystal_I2C*'
Display.cpp:18: error: request for member 'print' in '((Display*)this)->Display::lcd', which is of non-class type 'LiquidCrystal_I2C*'  

When i use the functions of the LCD object outside the class there's no problem. Am i passing the object wrong to the class?


回答1:


lcd is a pointer

  lcd.init();
  lcd.clear();
  lcd.print("Constructor");

Above lines should correct as follows:

 lcd->init();
  lcd->clear();
  lcd->print("Constructor");


来源:https://stackoverflow.com/questions/30412572/call-function-on-lcd-object-in-class

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!