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

标签 c++ arduino

下面是一些简单的编译错误代码:

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);

请注意,大小写在 C++ 中很重要。

关于c++ - 错误 : 'null' was not declared in this scope when setting integer to 0 - photon/arduino C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34190695/

相关文章:

c++ - 无法在 MFC 中的 CFile 中设置只读属性?

c++ - 模板成员函数上的外线 sfinae 是否可能?

c++ - Arduino WiFi101 库 - 将 WiFi 和 WiFiClient 传递给子类

c++ - 错误 : 'track_t' was not declared in this scope

if-statement - 当for循环内遇到if语句时跳过for循环

python - 使用串行端口和 MySQL 从 arduino 在 Rpi 上收集数据

c++ - C++识别文件类型

c++ - 在 C++ 中读取 pcap 文件

c++ - 为什么编译器调用第一个重载函数而不是第二个?

c++ - 如何使用移位运算符 << 分配 BH1750 光传感器中的值?