在类中的LCD对象上调用函数 [英] Call function on lcd object in class

查看:73
本文介绍了在类中的LCD对象上调用函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前,我正在制作一个类,用于将自定义文本打印到LCD.

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

我将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()
{
}

当我在构造函数中的lcd对象上调用函数时,不断出现这些错误.

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*'  

当我在课堂外使用LCD对象的功能时,没有问题.我将对象错误传递给班级吗?

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?

推荐答案

lcd是指针

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

以上各行应按如下所示进行纠正:

Above lines should correct as follows:

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

这篇关于在类中的LCD对象上调用函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆