我目前接收下列消息:非類類型的錯誤,和指針發出
錯誤從阿爾杜伊諾IDE接收:對構件「EEPROMWriteAny」在「ETYPE」 請求,其是非類類型「的EEPROMAnyType()'
我不認爲我通過引用正確地在我的代碼中傳遞變量,因爲這似乎是導致上面寫的錯誤的主要問題。 或者在一個類的類中有EEPROMWriteAny和EEPROM ReadAny是什麼導致這個錯誤?我必須指向EEPROMAnyType類,然後指向U類嗎? EEPROMReadAny甚至在嵌套類中嗎?
我使用EEPROM庫提供了頭文件,cpp源文件,arduino示例代碼以及如何定義下面類的關鍵字。 我也提供了一個圖片,說明我的庫在它的文件夾中的樣子,以防出現問題。
頭文件
#ifndef EEPROMAnyType_h
#define EEPROMAnyType_h
#include <Arduino.h>
#include <EEPROM.h>
class EEPROMAnyType
{
public:
template <class U> int EEPROMReadAny(unsigned int addr, U& x); //Reads any type of variable EEPROM
template <class U> int EEPROMWriteAny(unsigned int addr, U& x);//Writes any type of variable to EEPROM
private:
int i;
const byte *p[];
byte *p[];
};
#endif
CPP文件:
#include "Arduino.h"
#include "EEPROM.h"
#include "EEPROMAnyType.h"
template <class U> int EEPROMAnyType::EEPROMReadAny(unsigned int addr, U x)
{
int i;
byte* p[sizeof(x)] = (byte*)(void*) x; //(void*) makes variable x "typeless" and then (byte*) casts the typeless variable as a byte type
for(i = 0; i < sizeof(x); i++){ // Why can I not declare i as an integer in the for loop?
p[i]= EEPROM.read(addr+i);
}
return i;
}
template <class U> int EEPROMAnyType::EEPROMWriteAny(unsigned int addr, U x)
{
int i = 0;
const byte* p[i] = (const byte*)(const void*) x;
for(i = 0; i < sizeof(x); i++){
EEPROM.write(addr+i, p[i]);
}
return i;
}
的Arduino代碼:
#include <Arduino.h>
#include <EEPROM.h>
#include <EEPROMAnyType.h>
EEPROMAnyType eType(); // used to call EEPROMAnyType which can write/read any kind of data type to/from EERPOM
int addr = 10; //memory location to be pass by value to EEPROMAnyType functions
String *p;// To pass by reference to EEPROMAnyType functions
String x; //will be assigned to EEPROM location
void setup() {
x = "memory";
p = &x;
Serial.begin(9600);
Serial.println("Hello World"); //shows start point of code
eType.EEPROMWriteAny(addr, *p); //Writes to EEPROM memory 10
x = eType.EEPROMReadAny(addr, *p); //assigns bytes from EEPROM memory location to x
}
void loop() {
Serial.print("x: ");
Serial.println(x);
Serial.print("no. of bytes stored in x: ");
Serial.println(EEPROMWriteAny(addr, p));
delay(1000);
}
這裏是我定義的關鍵字爲類EPPROMAnyType:
EEPROMAnyType KEYWORD1
EEPROMReadAny KEYWORD2
EEPROMWriteAny KEYWORD2
'EEPROMAnyType eType();'應該是'EEPROMAnyType eType;'如果你想有一個實例。否則,你只是聲明一個名爲'eType'的函數。 –