问题
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