错误:将整数设置为 0 时,未在此范围内声明'null' - 光子/Arduino C++

error: 'null' was not declared in this scope when setting integer to 0 - photon/arduino C++

本文关键字:声明 null C++ Arduino 光子 范围内 设置 整数 错误      更新时间:2023-10-16

下面有一些简单的代码,使用编译时出错

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++中很重要。