2013-09-05 70 views
3

我已經寫了一個庫,但有錯誤的問題沒有命名一個類型。我嘗試了一切,搜索了幾個小時,沒有運氣。庫被放置在arduino草圖文件夾的「庫」文件夾中。請幫忙!!!我正在使用OSX,但同樣的問題也發生在Windows上。Arduino錯誤:不會命名一個類型?

這是庫的頭文件:

#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 

這是庫的.cpp文件:

#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; 
} 

而這正是包括庫.ino文件:

#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() { 

} 

這是錯誤:

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

+1

請參閱[Adafruit RGB LCD屏蔽常見問題解答](http://learn.adafruit.com/rgb-lcd-shield/f-dot-aq)第3項,並按照建議確保Arduino環境正確意識到Adafruit圖書館。 –

+0

這不僅僅是Adafruit RGB LCD庫問題,還是作爲Arduino核心庫一部分的SD.h庫的一部分的「文件」類型。實際上,它對每個非標準類型的類型都會產生類型錯誤,例如bool,int,String等 –

+0

啊,所以在庫中包含其他庫可能是一個問題。我必須嘗試重現這一點,我今天無法做到這點......(可能你正在用測試草圖進行構建 - 你可以嘗試將你的自定義庫和Adafruit_RGBLCDShield導入到Sketch中,並且看看Adafruit錯誤是否消失?) –

回答

8

這兩個包括你在你的評論中提到的是必不可少的。 '不命名類型'僅僅意味着編譯器不能定義該標識符。如果您提到的LCD庫存在錯誤,那麼需要解決這些問題 - 省略#include絕對不會解決它!從經驗

有兩點需要注意,這可能是有益的:

  1. 您需要添加所有#包括對主要的素描 - 不論他們是通過其他#include包含。

  2. 如果您將文件添加到庫文件夾,必須重新啓動Arduino IDE才能看到這些新文件。

+0

順便說一句我看你用布爾代替布爾。請注意,如果這很重要,但是。 –

+0

你的第一個注意事項(「將所有#include添加到主草圖」)適用於我。 –

2

安裝NeoMatrix庫時,出現了does not name a type錯誤。

解決方案:.cpp.h文件需要在頂層文件夾,當你複製它,例如:

當我使用默認的Windows解壓縮程序,它嵌套在另一個文件夾裏面的內容:

myArduinoFolder/libraries/Adafruit_NeoMatrix/Adafruit_NeoMatrix/Adafruit_NeoMatrix.cpp 

我感動的文件了,所以它是:

這固定了does not name a type的問題。

0

不知道這是你的問題,但它是我的。

空隙設置()沒有指定類型

BUT

空隙設置()是確定。

我發現我爲另一個項目複製的草圖充滿了'錯誤的大小寫'字母。 Onc並列,運行順利。強調文本

-1

一般頭文件的語法開始與資本letter.I發現,在較小的信寫的所有​​代碼

#ifndef DIAG_H 
#define DIAG_H 

#endif 
0

,你所要做的就是加入這一行唯一要 草圖

#include <SPI.h> 

之前#include <Adafruit_MAX31855.h>

0

最近,我發現其他因素也會導致這個錯誤。我有一個AES.h,AES.cpp包含一個AES類,它給了這同樣無益的錯誤。只有當我重命名爲Encryption.h,Encryption.cpp和Encryption作爲類名時,它才突然開始工作。沒有其他代碼更改。

相關問題