目前我正在制作一个用于将自定义文本打印到LCD的课程。
我将lcd对象作为构造函数中的参数传递给类。
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对象上的函数时,不断收到这些错误。
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对象的功能时没有问题。 我把对象错误传递给了班级吗?
答案 0 :(得分:1)
lcd是一个指针
lcd.init();
lcd.clear();
lcd.print("Constructor");
以上行应纠正如下:
lcd->init();
lcd->clear();
lcd->print("Constructor");