在我的Arduino库中使用外部库(Use external library in my Arduino library)

编程入门 行业动态 更新时间:2024-10-11 17:25:50
在我的Arduino库中使用外部库(Use external library in my Arduino library)

我想创建一个个人库,在那里我使用另一个。 在我的代码中,我在私有部分声明并初始化了库。

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

我写了几个版本,但没有任何改变。 我最多可以显示print Test01和test02 。

。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

I would like to create a personal library where I use another one. In my code I declared and initialized the library in the private part.

But I have the error '((LCD*)this)->LCD::lcd' does not have class type.

I've written several versions, but nothing changes. At best I can display the print Test01 and test02.

.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);
}
 

I'll give you the entire error message, too.

[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); }

You cannot initialize members in the class declaration. Try with:

class LCD { public: LCD(); void firstLine(); void secondLine(float tempInCelsius); private: LiquidCrystal_I2C lcd; };

But you can initialize such members (must, if they do not provide a default constructor) in the constructor(s). Try with:

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

更多推荐

本文发布于:2023-08-06 22:55:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1456353.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:库中   Arduino   library   external

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!