错误:将整数设置为 0 时,未在此范围内声明'null' - 光子/Arduino C++
error: 'null' was not declared in this scope when setting integer to 0 - photon/arduino C++
下面有一些简单的代码,使用编译时出错
scorer.cpp: In function 'void loop()':
scorer.cpp:56:37: error: 'null' was not declared in this scope
feeler2ConsecutivePresses = 0;
我尝试将其设置为NULL
而不是0,并包括标准库。我错过了什么(这里是C++初学者)
int feeler1 = D0;
int feeler2 = D2;
int batteryLowIndicator = D4;
int pingFrequency = 5000;
unsigned long lastPing = millis();
bool batteryLow = false;
bool sentOnlineMessage = false;
int feeler1PreviousState;
int feeler2PreviousState;
int batteryLevelPreviousState;
int feeler1ConsecutivePresses;
int feeler2ConsecutivePresses;
int consecutivePressThreshold = 30; // 3 seconds
void setup() {
pinMode(feeler1, INPUT_PULLDOWN);
pinMode(feeler2, INPUT_PULLDOWN);
pinMode(batteryLowIndicator, INPUT_PULLUP);
}
void loop() {
int feeler1Pressed = digitalRead(feeler1);
int feeler2Pressed = digitalRead(feeler2);
int batteryLevel = digitalRead(batteryLowIndicator);
if (Particle.connected() && !sentOnlineMessage) {
sentOnlineMessage = true;
Particle.publish("online", "1", 60, PRIVATE);
}
if (feeler1Pressed == HIGH) {
if (feeler1PreviousState == HIGH) {
feeler1ConsecutivePresses += 1;
} else {
Particle.publish("scored", "1", 60, PRIVATE);
}
} else {
feeler1ConsecutivePresses = 0;
}
if (feeler2Pressed == HIGH) {
if (feeler2PreviousState == HIGH) {
feeler2ConsecutivePresses += 1;
} else {
Particle.publish("scored", "2", 60, PRIVATE);
}
} else {
feeler2ConsecutivePresses = 0;
}
if (feeler1ConsecutivePresses == consecutivePressThreshold
&& feeler2ConsecutivePresses == consecutivePressThreshold) {
Particle.publish("endGame", null, 60, PRIVATE);
}
if (batteryLevel == LOW && batteryLevel != batteryLevelPreviousState) {
Particle.publish("batteryLow", "1", 60, PRIVATE);
}
// Ping server every x seconds
if (millis() - lastPing > pingFrequency) {
Particle.publish("ping", "1", 60, PRIVATE);
lastPing = millis();
}
feeler1PreviousState = feeler1Pressed;
feeler2PreviousState = feeler2Pressed;
batteryLevelPreviousState = batteryLevel;
delay(100);
}
在行中
Particle.publish("endGame", null, 60, PRIVATE);
您使用了一种名为null
的东西,而您和标准都没有声明这样的东西。
如果这应该传递一个空指针,请使用
Particle.publish("endGame", nullptr, 60, PRIVATE);
或者,如果您是C++11之前的版本,并且包含适当的库,
Particle.publish("endGame", NULL, 60, PRIVATE);
注意,case在C++中很重要。
相关文章:
- .cpp和.h文件中的模板专用化声明
- 未在作用域中声明unordered_map
- C++避免重复声明的语法是什么
- 如何确保C++函数在定义之前声明(如override关键字)
- 错误:未在此范围内声明'reverse'
- 奇怪的(对我来说)返回声明 - 在谷歌上找不到任何关于它的信息
- 如果我在相应 char 数组的声明中为其提供额外的元素,是否会自动设置 NULL?
- GNU 链接器:如果头文件中的指针被声明为 NULL 和/或 extern,则出错
- 一旦对象设置为 null,用于声明对象的堆大小的变量就设置为 null?
- 声明新节点但不等于 NULL
- C++ 模板 NULL 未声明的标识符
- 错误:未在此范围内声明"null"
- 错误:将整数设置为 0 时,未在此范围内声明'null' - 光子/Arduino C++
- 派生类的头文件中声明的字符串在超级类中为null
- 将 head 设置为 NULL ('NULL' : 未声明的标识符)
- 检查在模板第一个参数之后声明的函数是否为 NULL(传递向量和数组)
- 在此范围内未声明NULL
- 如何用null声明和初始化多维数组
- 使用前向声明编译时"null"?
- 如何声明一个函数,该函数创建一个永远不会为 null 的对象