我目前接收下列消息:非类类型的错误,和指针发出
错误从阿尔杜伊诺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'的函数。 –