我目前收到以下消息:
从Arduino IDE接收时出错:请求非类类型为“EEPROMAnyType()”的“eType”中的成员“”EEPROMWriteAny“”
我不认为我在我的代码中通过引用正确地传递变量,因为这似乎是导致上面写的错误的主要问题。或者是EEPROMWriteAny和EEPROM ReadAny在一个类中,是什么导致了这个错误?我是否必须先从EEPROMAnyType类指向,然后再指向U类?EEPROMReadAny是否在嵌套类中?
我已经提供了头文件,cpp源文件,使用EEPROM库的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[];
};
#endifCPP文件:
#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 KEYWORD2enter image description here
发布于 2016-04-23 17:19:20
EEPROMAnyType eType();-编译器会将此语句解释为函数声明,名称为eType,返回类型为EEPROMAnyType。因此,eType不是EEPROMAnyType的实例,任何类实例意义上的eType的使用都会产生错误。希望这能有所帮助。
https://stackoverflow.com/questions/36808188
复制相似问题