gpt4 book ai didi

c++ - 在我的 Arduino 库中使用外部库

转载 作者:太空狗 更新时间:2023-10-29 21:33:27 26 4
gpt4 key购买 nike

我想创建一个我使用另一个图书馆的个人图书馆。在我的代码中,我在私有(private)部分声明并初始化了库。

但是我有错误'((LCD*)this)->LCD::lcd' does not have class type

我写了几个版本,但没有任何改变。充其量我可以显示 print Test01test02

.h

#ifndef LCD_h
#define LCD_h

#include <LiquidCrystal_I2C.h>

class LCD{
public:
LCD();
void firstLine();
void secondLine(float tempInCelsius);

private:
LiquidCrystal_I2C lcd(0x27, 16, 2);
};

#endif

.cpp

#include "LCD.h"
#include <LiquidCrystal_I2C.h>

LCD::LCD(){
Serial.begin(9600);
Serial.println("Test 01");
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
}

void LCD::secondLine(float tempInCelsius){
Serial.println("Test 03");
lcd.clear();
lcd.setCursor(0, 1);
lcd.print("T = ");
lcd.print(tempInCelsius);
}

.ino

#include "LCD.h"


LCD CrystalLCD();

void setup(void)
{
Serial.begin(9600);
Serial.println("Test 02");
}

void loop(void)
{
CrystalLCD.secondLine(1.40);
}

我也会给你完整的错误信息。

[Starting] Verify sketch - arduino.ino
Loading configuration...
Initializing packages...
Preparing boards...
Verifying...
In file included from /Users/WorkSpace/Make/RucheChaufante/ino/LCD.cpp:1:0:
LCD.h:13: error: expected identifier before numeric constant
LiquidCrystal_I2C lcd(0x27, 16, 2);
^
LCD.h:13: error: expected ',' or '...' before numeric constant
/Users/WorkSpace/Make/RucheChaufante/ino/arduino.ino: In constructor 'LCD::LCD()':
arduino:9: error: '((LCD*)this)->LCD::lcd' does not have class type
Serial.println("Test 02");
^
arduino:10: error: '((LCD*)this)->LCD::lcd' does not have class type
}
^
arduino:11: error: '((LCD*)this)->LCD::lcd' does not have class type

^
/Users/WorkSpace/Make/RucheChaufante/ino/arduino.ino: In member function 'void LCD::secondLine(float)':
arduino:16: error: '((LCD*)this)->LCD::lcd' does not have class type
arduino:17: error: '((LCD*)this)->LCD::lcd' does not have class type
arduino:18: error: '((LCD*)this)->LCD::lcd' does not have class type
arduino:19: error: '((LCD*)this)->LCD::lcd' does not have class type
/Users/WorkSpace/Make/RucheChaufante/ino/arduino.ino: In function 'void loop()':
arduino:14: error: request for member 'secondLine' in 'CrystalLCD', which is of non-class type 'LCD()'
CrystalLCD.secondLine(1.40);
^
exit status 1
[Error] Exit with code=1

最佳答案

您不能在类声明中初始化成员。尝试:

class LCD
{
public:
LCD();
void firstLine();
void secondLine(float tempInCelsius);

private:
LiquidCrystal_I2C lcd;
};

但是您可以在构造函数中初始化此类成员(必须,如果它们不提供默认构造函数)。尝试:

LCD::LCD() : lcd(0x27, 16, 2) {
Serial.begin(9600);
Serial.println("Test 01");
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
}

关于c++ - 在我的 Arduino 库中使用外部库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51052223/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com