Detector.cpp:
#include "Detector.h"
void detectorSniffer(uint8_t *buf, uint16_t len) {
if(buf[12] == 0xA0 || buf[12] == 0xC0) {
c++; //Failing to read C long in the detector class
}
};
void Detector::start() {
Serial.print("start");
}
Detector.h
#include <ESP8266WiFi.h>
#include "Mac.h"
class Detector {
public:
void start();
unsigned long c = 0; // Variable
};
我试图从Detector
函数访问detectorSniffer()
类中的无符号长变量。我曾Detector d
与d.c
等人进行过尝试。我做错了什么?
答案 0 :(得分:3)
如果要修改类(成员)变量,则必须首先创建此类的实例。
然后,最好的方法是将detectorSniffer
设置为类Detector
的方法(或朋友函数),之后您可以访问&amp;从detectorSniffer
修改其成员变量。
void Detector::detectorSniffer(uint8_t *buf, uint16_t len)
{
if(buf[12] == 0xA0 || buf[12] == 0xC0) {
c++;
}
};
// Calling
Detector d;
d.start();
d.detectorSniffer(/*parameters*/);
或传递对Detector
void Detector::detectorSniffer(uint8_t *buf, uint16_t len, Detector & d) {
if(buf[12] == 0xA0 || buf[12] == 0xC0) {
d.c++; //Failing to read C long in the detector class
}
};
// Calling
Detector d;
d.start();
detectorSniffer(/*some parameters*/, d);