Arduino的错误:没有指定类型? [英] Arduino error: does not name a type?

查看:544
本文介绍了Arduino的错误:没有指定类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个库,但有出错的问题没有指定类型。我trye​​d一切,搜索了几个小时,没有运气。库放置在Arduino的草图文件夹的库文件夹中。请帮忙!!!我使用OSX,但在Windows出现同样的问题也。

这是库的头文件:

 的#ifndef OpticalSensor_h
#定义OpticalSensor_h#包括LT&;&Arduino.h GT;
#包括LT&;&SD.h GT;
#包括LT&;&Wire.h GT;
#包括LT&;&Adafruit_MCP23017.h GT;
#包括LT&;&Adafruit_RGBLCDShield.h GT;
#包括LT&;&STRING.H GT;类OpticalSensor
{
    上市:
        OpticalSensor(INT analogPort);
        无效LCDInit(INT列,诠释行);
        无效SerialInit(INT比特率);
        无效SDInit();
        双功放&; ReadFromAnalogPort();
        无效SDCreateFile(字符串文件名);
        无效SDDeleteFile(字符串文件名);
        无效SDWriteToFile(字符串文件名);
        无效SDStreamToFile(字符串文本);
        无效SDOpenFileToStream(字符串文件名);
    私人的:
        INT _analogPort;
        布尔_displayFlag;
        Adafruit_RGBLCDShield _lcd;
        文件_MainRecFile;
        双_voltage;
        无效_LCDClearAll();
        无效_LCDWriteInTwoRows(ROW1字符串,字符串ROW2);
        无效_DelayAndClearLCD(布尔returnStatus);
};#万一

这是图书馆的.cpp文件:

 的#include< OpticalSensor.h>Adafruit_RGBLCDShield _lcd;
文件_MainRecFile;
双_voltage;OpticalSensor :: OpticalSensor(INT analogPort)
{
    _analogPort = analogPort;
}无效OpticalSensor :: LCDInit(INT列,诠释行)
{    _lcd = Adafruit_RGBLCDShield();
    _lcd.begin(列,行);
}无效OpticalSensor :: SerialInit(INT比特率)
{
    Serial.begin(比特率);
    _bitRate =比特率;
    而(!串行){
        //等到串行未打开
    }
}无效OpticalSensor :: SDInit()
{
    //在以太网盾,CS引脚4,它设置为默认的输出。
    //注意,即使它不是作为CS引脚,硬件SS引脚
    //(10月最Arduino的板,53兆),必须保留作为输出
    //或者SD库函数将无法正常工作。
    pinMode(10,输出);    //检查是否SD可以被发现和初始化。打印也讯息
    //如果串口初始化,如果初始化为_lcd。
   如果(!SD.begin(4)){
     如果(串行){
         Serial.println(初始化失败!);
     }
     如果(_lcd){
         _lcd.print(初始化失败!);
     }
     _DelayAndClearLCD(真);
   }
   其他{
       如果(串行){
           Serial.println(初始化完成了!);
       }
       如果(_lcd){
           lcd.print(初始化完成了!);
       }
       _DelayAndClearLCD(假);
   }
}无效OpticalSensor :: SDCreateFile(字符串文件名)
{
    //检查文件是否存在媒体链接,如果没有它会创建一个
    //和写入apropriate响应
    // LCD和串行如果他们被初始化。
    如果(SD.exists(文件名)){
        如果(串行){
            Serial.println(文件名+已经存在!);
        }
        如果(_lcd){
            _LCDWriteInTwoLines(文件名,已经存在!);
        }
        _DelayAndClearLCD(假);
    }
    其他
    {
        如果(串行){
            Serial.println(文件名+创建文件+文件名+...);
        }
        如果(_lcd){
            _LCDWriteInTwoLines(创建文件,文件名);
        }
        _MainRecFile = SD.open(文件名+名.txt,FILE_WRITE);
        _MainRecFile.close();
        _DelayAndClearLCD(假);
        //检查文件successffully和打印apropriate响应创建
        //为LCD和串行,如果他们被初始化
        如果(SD.exists(文件名+名.txt)){
            如果(串行){
                Serial.println(文件名+名.txt+创建successffully!);
            }
            如果(_lcd){
                _LCDWriteInTwoLines(文件名+名.txt,创建!);
            }
            _DelayAndClearLCD(假);
        }
        其他{
            如果(串行){
                Serial.println(错误:无法创建文件!);
            }
            如果(_lcd){
                _LCDWriteInTwoLines(错误:没有,创建文件!);
            }
            _DelayAndClearLCD(假);
        }
    }
}//删除SD卡文件
无效OpticalSensor :: SDDe​​leteFile(字符串文件名)
{}//打开文件,写入后数据给它,并关闭文件。
无效OpticalSensor :: SDWriteToFile(字符串文件名,字符串文本)
{
    _MainRecFile = SD.open(文件名+名.txt,FILE_WRITE);
    _MainRecFile.println(文本);
    _MainRecFile.close();
}//打开文件流数据到它。
无效OpticalSensor :: SDOpenFileToStream(字符串文件名)
{
    _MainRecFile = SD.open(文件名+名.txt,FILE_WRITE);
}//将数据写入文件,而文件是打开的。
//注意,您可以在同一时间的数据流只在一个文件!
//例如,如果你有两个传感器要
//写数据到两个不同的文件,你必须使用SDWriteToFile
//功能!!!
无效OpticalSensor :: SDStreamToFile(字符串文本)
{
    如果(_MainRecFile){
        _MainRecFile.println(文本);
    }
}//你的数据流太近文件。
无效OpticalSensor :: SDCloseStreaming(字符串文件名)
{
    _MainRecFile.close();
}//明确整个LCD
无效OpticalSensor :: _ LCDClearAll()
{
    _lcd.clear();
    _lcd.setCursor(0,0);
}无效OpticalSensor :: _ LCDWriteInTwoRows(ROW1字符串,字符串ROW2)
{
    //写ROW1第一个String
    _lcd.print(ROW1);
    //设置光标到行2的开始
    _lcd.setCursor(0,1);
    //写第二个字符串排2
    _lcd.print(ROW2);
}无效OpticalSensor :: _ DelayAndClearLCD(布尔returnStatus)
{
    //如果串行或_lcd初始化,延迟2秒
    //和清晰的LCD
    如果(串行|| _lcd){
        延迟(2000);
        如果(_lcd)
            _LCDClearAll();
    }
    //终止
    如果(布尔==真){
        返回;
    }
}双功放&; ReadFromAnalogPort()
{
    _voltage = analogRead(_analogPort);
    返回_voltage;
}

而这正是包括库中的文件.ino:

 的#include< OpticalSensor.h>OpticalSensor传感器(0);无效设置(){
  sensor.LCDInit(16,2);
  sensor.SerialInit(9600);
  sensor.SDInit();
  sensor.SDCreateFile(测试1);
  sensor.SDOpenFileToStream(测试1);
}空隙环(){}

这是错误:


  

在从Test_OpticalSensorLib.ino包含的文件:1:
  /Users/gaspersladic/Documents/Arduino/libraries/OpticalSensor/OpticalSensor.h:34:
  错误:Adafruit_RGBLCDShield没有指定类型
  /Users/gaspersladic/Documents/Arduino/libraries/OpticalSensor/OpticalSensor.h:35:
  错误:文件没有指定类型



解决方案

这两个包含您在您的评论何况是必不可少的。 没有指定类型'只是意味着有该标识的编译器可见没有定义。如果你提到的LCD库有错误,那么这些需要解决的问题! - 省略的#include肯定不会解决它。

从经验有两点需要注意这可能是有益的:


  1. 您需要添加所有#包括对主要的草图 - 无论他们是否通过其它#include包含


  2. 如果您将文件添加到库文件夹,Arduino的IDE之前一定要这些新文件重新启动将是可见的。


I have written a library, but have problem with error does not name a type. I've tryed everything, searched for couple of hours and no luck. Library is placed in the "libraries" folder of the arduino sketch folder. Please help!!! I am using OSX, but the same problem occurs on Windows also.

This is header file of the library:

#ifndef OpticalSensor_h
#define OpticalSensor_h

#include <Arduino.h>
#include <SD.h>
#include <Wire.h>
#include <Adafruit_MCP23017.h>
#include <Adafruit_RGBLCDShield.h>
#include <String.h>

class OpticalSensor
{
    public:
        OpticalSensor(int analogPort);
        void LCDInit(int columns, int rows);
        void SerialInit(int bitRate);
        void SDInit();
        double& ReadFromAnalogPort();
        void SDCreateFile(String fileName);
        void SDDeleteFile(String fileName);
        void SDWriteToFile(String fileName);
        void SDStreamToFile(String Text);
        void SDOpenFileToStream(String fileName);
    private:
        int _analogPort;
        bool _displayFlag;
        Adafruit_RGBLCDShield _lcd;
        File _MainRecFile;
        double _voltage;
        void _LCDClearAll();
        void _LCDWriteInTwoRows(String row1, String row2);
        void _DelayAndClearLCD(bool returnStatus);
};

#endif

This is .cpp file of the library:

#include <OpticalSensor.h>

Adafruit_RGBLCDShield _lcd;
File _MainRecFile;
double _voltage;

OpticalSensor::OpticalSensor(int analogPort)
{
    _analogPort = analogPort;
}

void OpticalSensor::LCDInit(int columns, int rows)
{

    _lcd = Adafruit_RGBLCDShield();
    _lcd.begin(columns,rows);
}

void OpticalSensor::SerialInit(int bitRate)
{
    Serial.begin(bitRate);
    _bitRate = bitRate;
    while(!Serial) {
        //wait until serial is not open
    }
}

void OpticalSensor::SDInit()
{
    // On the Ethernet Shield, CS is pin 4. It's set as an output by default.
    // Note that even if it's not used as the CS pin, the hardware SS pin
    // (10 on most Arduino boards, 53 on the Mega) must be left as an output
    // or the SD library functions will not work.
    pinMode(10, OUTPUT);

    //check if SD can be found and initialized. Print also message to
    //Serial if initialized and to _lcd if initialized.
   if(!SD.begin(4)) {
     if(Serial){
         Serial.println("Initialization failed!");
     }
     if(_lcd){
         _lcd.print("Init failed!");
     }
     _DelayAndClearLCD(true);
   }
   else {
       if(Serial) {
           Serial.println("Initialization done!");
       }
       if(_lcd) {
           lcd.print("Init done!");
       }
       _DelayAndClearLCD(false);
   }
}

void OpticalSensor::SDCreateFile(String fileName)
{
    //check if file allready exists, if not it creates one
    //and writes apropriate response to
    //lcd and Serial if they are initialized.
    if(SD.exists(fileName)) {
        if(Serial) {
            Serial.println(fileName + " already exists!");
        }
        if(_lcd) {
            _LCDWriteInTwoLines(fileName,"already exists!");
        }
        _DelayAndClearLCD(false);
    }
    else
    {
        if(Serial) {
            Serial.println(fileName + "Creating file " + fileName + "...");
        }
        if(_lcd) {
            _LCDWriteInTwoLines("Creating file", fileName);
        }
        _MainRecFile = SD.open(fileName + ".txt", FILE_WRITE);
        _MainRecFile.close();
        _DelayAndClearLCD(false);


        //check if file was created successffully and print apropriate response
        //to lcd and Serial if they are initialized
        if(SD.exists(fileName + ".txt")) {
            if(Serial) {
                Serial.println(fileName + ".txt" + " created successffully!");
            }
            if(_lcd) {
                _LCDWriteInTwoLines(fileName + ".txt", "created!");
            }
            _DelayAndClearLCD(false);
        }
        else {
            if(Serial) {
                Serial.println("error: failed to create file!");
            }
            if(_lcd) {
                _LCDWriteInTwoLines("error: failed to","create file!");
            }
            _DelayAndClearLCD(false);
        }
    }
}

//delete file from SD card
void OpticalSensor::SDDeleteFile(String fileName)
{

}

//open file, write data to it, and close file after.
void OpticalSensor::SDWriteToFile(String fileName, String Text)
{
    _MainRecFile = SD.open(fileName + ".txt", FILE_WRITE);
    _MainRecFile.println(Text);
    _MainRecFile.close();
}

//Open file to stream data to it.
void OpticalSensor::SDOpenFileToStream(String fileName)
{
    _MainRecFile = SD.open(fileName + ".txt", FILE_WRITE);
}

//Write data to file while file is open.
//Notice that you can stream data only to one file at a time!!!
//For instance, if you have two sensors that you want to
//write data to two different files, you have to use SDWriteToFile
//function!!!
void OpticalSensor::SDStreamToFile(String Text)
{
    if(_MainRecFile) {
        _MainRecFile.println(Text);
    }
}

//close file that you streamed data too.
void OpticalSensor::SDCloseStreaming(String fileName)
{
    _MainRecFile.close();
}

//clear entire LCD
void OpticalSensor::_LCDClearAll()
{
    _lcd.clear();
    _lcd.setCursor(0,0);
}

void OpticalSensor::_LCDWriteInTwoRows(String row1, String row2)
{
    //write first String in row1
    _lcd.print(row1);
    //set cursor to the beginning of row 2
    _lcd.setCursor(0,1);
    //write second String to row 2
    _lcd.print(row2);
}

void OpticalSensor::_DelayAndClearLCD(bool returnStatus)
{
    //if Serial or _lcd are initialized, delay for 2 seconds
    //and clear LCD
    if(Serial || _lcd) {
        delay(2000);
        if(_lcd)
            _LCDClearAll();
    }
    //terminate
    if(bool == true) {
        return;
    }
}

double& ReadFromAnalogPort()
{
    _voltage = analogRead(_analogPort);
    return _voltage;
}

And this is the .ino file where library is included:

#include <OpticalSensor.h>

OpticalSensor sensor(0);

void setup() {
  sensor.LCDInit(16,2);
  sensor.SerialInit(9600);
  sensor.SDInit();
  sensor.SDCreateFile("test1");
  sensor.SDOpenFileToStream("test1");
}

void loop() {

}

this is the error:

In file included from Test_OpticalSensorLib.ino:1: /Users/gaspersladic/Documents/Arduino/libraries/OpticalSensor/OpticalSensor.h:34: error: 'Adafruit_RGBLCDShield' does not name a type /Users/gaspersladic/Documents/Arduino/libraries/OpticalSensor/OpticalSensor.h:35: error: 'File' does not name a type

解决方案

The two includes you mention in your comment are essential. 'does not name a type' just means there is no definition for that identifier visible to the compiler. If there are errors in the LCD library you mention, then those need to be addressed - omitting the #include will definitely not fix it!

Two notes from experience which might be helpful:

  1. You need to add all #include's to the main sketch - irrespective of whether they are included via another #include.

  2. If you add files to the library folder, the Arduino IDE must be restarted before those new files will be visible.

这篇关于Arduino的错误:没有指定类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆