ntwhq 发表于 2013-8-25 07:08:28

写Arduino扩展库编译时出错,求教。

本帖最后由 ntwhq 于 2013-8-25 10:29 编辑

         编程时想自己写一个库,但编译时总是出错,找不出原因,我就把为LED闪烁程序做试验写了一个最简单的库,但编译时还是出现同样的错误,请大家帮我找找原因。
      两个库文件:
      LED.h:

#ifndef LED_h
#define LED_h
#if ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
class LED
{
byte _pin;
public:
LED(byte pin);
void on();
void off();
};
#endif

LED.cpp:

#include "LED.h"
LED::LED(byte pin)
{
      _pin=pin;
      pinMode(pin,OUTPUT);
}
void LED::on()
{
      digitalWrite(_pin,HIGH);
}

void LED::off()
{
      digitalWrite(_pin,LOW);
}

程序代码:

#include <LED.h>
void setup()
{
LED.LED(13);
}
void loop()
{
    LED.on();
    delay(1000);
    LED.off();
    delay(1000);
}

编译后出现如下界面:



出错信息为:
LED.ino: In function 'void setup()':
LED:4: error: expected unqualified-id before '.' token
LED.ino: In function 'void loop()':
LED:8: error: expected unqualified-id before '.' token
LED:10: error: expected unqualified-id before '.' token

大致意思是 .前的LED不合格(不合法),反复试了也查不出错误的原因,看看和其它一些扩展库的结构也没有什么不同,就是不能正常运行,请各位高手指教一下错在哪里,谢谢!

pathletboy 发表于 2013-8-25 09:18:05

需要申明对象#ifndef LED_h
#define LED_h
#if ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
class LED
{
byte _pin;
public:
LED(byte pin);
void on();
void off();
}led;
#endif然后应用程序里大写的LED改小写。

ntwhq 发表于 2013-8-25 09:51:16

本帖最后由 ntwhq 于 2013-8-25 09:57 编辑

pathletboy 发表于 2013-8-25 09:18 static/image/common/back.gif
需要申明对象然后应用程序里大写的LED改小写。

谢谢!这样改了没有用。空了您帮我试一下,谢谢!



pathletboy 发表于 2013-8-25 09:59:45

哦,构造函数不该由你调用,另外写个函数设置pin,或者你手工申明对象。    #ifndef LED_h
    #define LED_h
    #if ARDUINO >= 100
    #include "Arduino.h"
    #else
    #include "WProgram.h"
    #endif
    class LED
    {
      byte _pin;
      public:
      LED(byte pin);
      void on();
      void off();
    };
    #endif    #include <LED.h>
    LED led(13);
    void setup()
    {

    }
    void loop()
    {
      led.on();
      delay(1000);
      led.off();
      delay(1000);
    }

ntwhq 发表于 2013-8-25 10:15:32

可以了,十分感谢!不明白为什么要改小写,而且 LED led(13)中间不能加"."

pathletboy 发表于 2013-8-25 10:20:25

ntwhq 发表于 2013-8-25 10:15 static/image/common/back.gif
可以了,十分感谢!不明白为什么要改小写,而且 LED led(13)中间不能加"."

这个属于c/c++语法,建议你学习下,LED是你自己申明的类,然后申明一个类对象led,c/c++语法规定,大小写敏感,也就是LED和led是2个不同的标识符。

ntwhq 发表于 2013-8-25 10:31:43

本帖最后由 ntwhq 于 2013-8-25 10:34 编辑

pathletboy 发表于 2013-8-25 10:20 static/image/common/back.gif
这个属于c/c++语法,建议你学习下,LED是你自己申明的类,然后申明一个类对象led,c/c++语法规定,大小写 ...

谢谢!我知道大小写有区别的,我库里是大写,怎么到程序里就要改成小写呢?对象led(小写)在哪里申明了?是这一句“LED led(13)”附带申明了?

pathletboy 发表于 2013-8-25 10:33:22

ntwhq 发表于 2013-8-25 10:31 static/image/common/back.gif
谢谢!我知道大小写有区别的,我库里是大写,怎么到程序里就要改成小写呢?

你库里的是你自己申明的“类”,你操作不能操作类,要操作类的对象,所以你要申明一个类的对象叫做led。

ntwhq 发表于 2013-8-25 10:35:37

本帖最后由 ntwhq 于 2013-8-25 10:36 编辑

pathletboy 发表于 2013-8-25 10:33 static/image/common/back.gif
你库里的是你自己申明的“类”,你操作不能操作类,要操作类的对象,所以你要申明一个类的对象叫做led。

对象led是自动形成的?是这一句“LED led(13)”附带申明了对象led?

pathletboy 发表于 2013-8-25 10:40:08

ntwhq 发表于 2013-8-25 10:35 static/image/common/back.gif
对象led是自动形成的?是这一句“LED led(13)”附带申明了对象led?

需要你自己申明创建对象,你把c++基本语法看下就明白了。

ntwhq 发表于 2013-8-25 10:42:15

本帖最后由 ntwhq 于 2013-8-25 14:54 编辑

pathletboy 发表于 2013-8-25 10:40 static/image/common/back.gif
需要你自己申明创建对象,你把c++基本语法看下就明白了。

好的,我自己再去学习,谢谢您!

ntwhq 发表于 2017-3-30 20:31:44

本帖最后由 ntwhq 于 2017-3-30 20:52 编辑

               回复错了
页: [1]
查看完整版本: 写Arduino扩展库编译时出错,求教。