我正在上课:
class SerialLoad {
public:
SerialLoad(int line, int total, LiquidCrystal_I2C lcd);
void increment();
void print();
void clearLine();
private:
int line;
int current;
int total;
int percent;
LiquidCrystal_I2C lcd;
};
正如你所看到的,我想传递一个将在其他地方创建的LCD,但它在这里给我一个错误,因为我做不到" LiquidCrystal_I2C lcd;"没有创建一个新的。我该怎么办呢?
我想做的只是传递一个......
这是我得到的错误:
C:\Users\Josh\Documents\Arduino\libraries\SerialLoad\SerialLoad.cpp: In constructor 'SerialLoad::SerialLoad(int, int, LiquidCrystal_I2C)':
C:\Users\Josh\Documents\Arduino\libraries\SerialLoad\SerialLoad.cpp:5: error: no matching function for call to 'LiquidCrystal_I2C::LiquidCrystal_I2C()'
C:\Users\Josh\Documents\Arduino\libraries\LiquidCrystal/LiquidCrystal_I2C.h:93: note: candidates are: LiquidCrystal_I2C::LiquidCrystal_I2C(uint8_t, uint8_t, uint8_t, uint8_t, uint8_t, uint8_t, uint8_t, uint8_t, uint8_t, t_backlighPol)
C:\Users\Josh\Documents\Arduino\libraries\LiquidCrystal/LiquidCrystal_I2C.h:89: note: LiquidCrystal_I2C::LiquidCrystal_I2C(uint8_t, uint8_t, uint8_t, uint8_t, uint8_t, uint8_t, uint8_t, uint8_t)
C:\Users\Josh\Documents\Arduino\libraries\LiquidCrystal/LiquidCrystal_I2C.h:70: note: LiquidCrystal_I2C::LiquidCrystal_I2C(uint8_t, uint8_t, uint8_t, uint8_t, uint8_t, t_backlighPol)
C:\Users\Josh\Documents\Arduino\libraries\LiquidCrystal/LiquidCrystal_I2C.h:67: note: LiquidCrystal_I2C::LiquidCrystal_I2C(uint8_t, uint8_t, uint8_t, uint8_t)
C:\Users\Josh\Documents\Arduino\libraries\LiquidCrystal/LiquidCrystal_I2C.h:53: note: LiquidCrystal_I2C::LiquidCrystal_I2C(uint8_t, uint8_t, t_backlighPol)
C:\Users\Josh\Documents\Arduino\libraries\LiquidCrystal/LiquidCrystal_I2C.h:51: note: LiquidCrystal_I2C::LiquidCrystal_I2C(uint8_t)
C:\Users\Josh\Documents\Arduino\libraries\LiquidCrystal/LiquidCrystal_I2C.h:39: note: LiquidCrystal_I2C::LiquidCrystal_I2C(const LiquidCrystal_I2C&)
答案 0 :(得分:0)
你的问题对于arduino来说真的不是很具体,而是一个非常普遍的c ++问题。 为了存储在您的课程之外创建的对象,您可以例如存储指向对象的指针,而不是
LiquidCrystal_I2C lcd;
你应该
LiquidCrystal_I2C *lcd;
在类的构造函数中,您必须相应地更改内容并将指针指定给类的成员。
SerialLoad(int line, int total, LiquidCrystal_I2C *outsideLcd)
{
lcd = outsideLcd;
}
另外几条评论:
根据您创建LiquidCrystal_I2C
对象的方式,您需要以不同方式将其传递给类。
如果你这样做
LiquidCrystal_I2C lcd;
你必须这样做
SerialLoad s([other variables here], &lcd);
这意味着您将lcd
对象的地址传递给构造函数。
如果您使用new创建LiquidCrystal_I2C
,则只需执行
SerialLoad s([other variables here], lcd);
您必须确保的一件事是,在您的课程中停止使用之前,您在课堂外创建的LiquidCrystal_I2C
不会被销毁,否则您的代码会在您尝试访问其中时崩溃你的班级。
如果这有帮助,请告诉我。正如开头所说的那样,这根本不是arduino特有的,而是纯粹的c ++。