我看到在这个主题中有几个Q& A但我仍然无法找到问题的线索。我必须承认我对c ++很新,所以这可能是根本原因。
好的,所以我试图声明一个新类型:
struct measurement_t
{
int sensorID;
int sensorData;
measurement_t(int ID, int Data)
{
sensorID = ID;
sensorData = Data;
}
};
我的项目包含一个方法,应该返回这种类型的数据:
measurement_t getLightSensorData()
{
...
}
当我尝试编译代码时出现错误:错误:' measurement_t'没有命名类型
我也觉得非常奇怪的是,编译器报告的错误行是代码最开头的第一个#define语句的行号:
#define CE_PIN 7
如果我只是将方法更改为void,则项目编译(无用)。
我也尝试通过指定measurement_t *作为返回类型来返回指针,但结果是相同的。尝试从struct中删除构造函数,再次得到相同的结果。
有人可以帮我理解这里的问题吗?
复制相同问题的代码的最小版本是:
struct measurement_t
{
int sensorID;
int sensorData;
measurement_t(int ID, int Data)
{
sensorID = ID;
sensorData = Data;
}
};
measurement_t getLightSensorData()
{
int sensorValue = 1;
measurement_t m(1, sensorValue);
return m;
}
作为参考,这是一个arduino草图,完整代码如下:
#include "RF24.h"
#include <SPI.h>
#include "printf.h"
#define CE_PIN 7
#define CS_PIN 8
#define LIGHTSENSOR_PIN A0
RF24 myRadio(CE_PIN, CS_PIN);
byte rxAddr[6] = { 0x0, 0x0, 0x0, 0x0, 0x0F };
unsigned long currentMillis;
unsigned long prevMillis;
unsigned long txIntervalMillis = 3000; // send once per second
unsigned long loopSleepMillis = 200;
struct measurement_t
{
int sensorID;
int sensorData;
measurement_t(int ID, int Data)
{
sensorID = ID;
sensorData = Data;
}
};
// the setup routine runs once when you press reset:
void setup()
{
Serial.println(">>> Initializing...");
Serial.begin(115200);
printf_begin();
bool radioOk = myRadio.begin(); // Start up the physical nRF24L01 Radio
if (radioOk) Serial.println(" Radio initialized");
else Serial.println(" ERROR initializing radio !");
myRadio.setChannel(120); // Above most Wifi Channels
myRadio.setPALevel(RF24_PA_HIGH);
myRadio.setRetries(15, 15);
myRadio.openWritingPipe(rxAddr); // Use the first entry in array 'addresses' (Only 1 right now)
Serial.println("<<< Done initialization");
}
// the loop routine runs over and over again forever:
void loop()
{
currentMillis = millis();
if (currentMillis - prevMillis >= txIntervalMillis)
{
transmitData(getLightSensorData());
prevMillis = millis();
}
delay(loopSleepMillis);
}
measurement_t getLightSensorData()
{
int sensorValue = analogRead(LIGHTSENSOR_PIN);
measurement_t m(1, sensorValue);
return m;
}
void transmitData(measurement_t data)
{
myRadio.stopListening();
Serial.print("Transmitting data...");
bool writeOK = myRadio.write(&data, sizeof(data));
if (writeOK)
{
Serial.println("OK");
}
else
{
Serial.println(F("no response"));
//myRadio.printDetails();
}
}
答案 0 :(得分:-1)
你必须使用
struct measurement_t getLightSensorData()
{
int sensorValue = analogRead(LIGHTSENSOR_PIN);
measurement_t m(1, sensorValue);
return m;
}
你可能有过时的Arduino IDE版本,因为在我的IDE上它编译时没有使用struct
关键字。