我已经编写了一个库,但是有错误的问题没有命名类型。我已经尝试了一切,搜索了几个小时,没有运气。库位于arduino sketch文件夹的“libraries”文件夹中。请帮忙!!!我使用的是OSX,但Windows上也会出现同样的问题。
这是库的头文件:
#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() {
}
这是错误:
Test_OpticalSensorLib.ino中包含的文件:1: /Users/gaspersladic/Documents/Arduino/libraries/OpticalSensor/OpticalSensor.h:34: 错误:'Adafruit_RGBLCDShield'未命名类型 /Users/gaspersladic/Documents/Arduino/libraries/OpticalSensor/OpticalSensor.h:35: 错误:'文件'未命名类型
答案 0 :(得分:10)
这两个包括你在评论中提到的必不可少的。 '不命名类型'只是意味着编译器看不到该标识符的定义。如果您提到的LCD库中存在错误,那么需要解决这些问题 - 省略#include肯定无法修复它!
经验中的两个注释可能会有所帮助:
您需要将所有#include添加到主草图中 - 无论它们是否包含在另一个#include中。
如果您将文件添加到库文件夹,则必须重新启动Arduino IDE才能看到这些新文件。
答案 1 :(得分:2)
安装NeoMatrix库时出现does not name a type
错误。
解决方案:复制时,.cpp
和.h
文件必须位于顶层文件夹中,例如:
myArduinoFolder/libraries/Adafruit_NeoMatrix/Adafruit_NeoMatrix.cpp
当我使用默认的Windows解压缩程序时,它将内容嵌套在另一个文件夹中:
myArduinoFolder/libraries/Adafruit_NeoMatrix/Adafruit_NeoMatrix/Adafruit_NeoMatrix.cpp
我移动了文件,所以它是:
myArduinoFolder/libraries/Adafruit_NeoMatrix/Adafruit_NeoMatrix.cpp
这解决了does not name a type
问题。
答案 2 :(得分:0)
不知道这是你的问题,但它是我的。
Void setup()未命名类型
BUT
void setup()没问题。
我发现我为另一个项目复制的草图充满了“错误的案例”字母。 Onc efixed,它运行顺利。强调文本
答案 3 :(得分:0)
您唯一需要做的就是添加此行 你的草图
#include <SPI.h>
在#include <Adafruit_MAX31855.h>
之前。
答案 4 :(得分:0)
最近,我发现其他因素也会导致此错误。我有一个AES.h,AES.cpp包含一个AES类,它给出了同样无益的错误。只有当我重命名为Encryption.h,Encryption.cpp和Encryption作为类名时才突然开始工作。没有其他代码更改。
答案 5 :(得分:0)
我的代码在Arduino中超出了void setup()或void loop()的限制。
答案 6 :(得分:0)
我在“}”中找到了解决此问题的方法。我对草图进行了一些更改,但忘记了检查“}”,因此又多了一个。我删除并编译后,一切都很好。
答案 7 :(得分:-1)
通常Header文件语法以大写字母开头。我发现代码全部用小写字母写成
#ifndef DIAG_H
#define DIAG_H
#endif