我正在学习如何使用C++中的 "类",当我试图从我的类中调用一个函数时,我得到了这个错误。
错误C:Users/Mateus/Documents/ArduinoData/packages/esp8266/hardware/esp8266/2.5.0/libraries/ESP8266WebServer/srcESP8266WebServer.h:90:8:注意:void ESP8266WebServer::on(const String&, HTTPMethod, ESP8266WebServer::THandlerFunction, ESP8266WebServer::THandlerFunction)
void on(const String &uri, HTTPMethod method, THandlerFunction fn, THandlerFunction ufn);
^
C:Users/Mateus/Documents/ArduinoData/packages/esp8266/hardware/esp8266/2.5.0/libraries/ESP8266WebServer/srcESP8266WebServer.h:90:8: note: candidate expects 4 arguments, 2 provided
exit status 1 调用'ESP8266WebServer::on(const char [6], )'时没有匹配函数。
我的代码。
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
ESP8266WebServer server;
class Light {
public:
int InPin;
int OutPin;
Light(int aInPin, int aOutPin){
InPin = aInPin;
OutPin = aOutPin;
}
void setPin() {
pinMode(InPin, INPUT_PULLUP);
pinMode(OutPin, OUTPUT);
}
void toggleLight()
{
digitalWrite(InPin,!digitalRead(InPin));
String lstate = digitalRead(InPin) ? "Status: ON" : "Status: OFF";
server.send(200,"text/html", lstate);
}
};
char* ssid = "MaIs2";
char* password = "3134767570";
const char index_html[] PROGMEM ={"HUGE html string"};
const char style_css[] PROGMEM = {"HUGE css string"};
int Door = 4;
int BDoor = 14;
Light l1(0, 16);
Light l2(2, 5);
void setup()
{
WiFi.begin(ssid,password);
Serial.begin(9600);
while(WiFi.status()!=WL_CONNECTED)
{
Serial.print(".");
delay(500);
}
Serial.println("");
Serial.print("IP Address: ");
Serial.println(WiFi.localIP());
l1.setPin();
l2.setPin();
server.on("/",[](){server.send_P(200,"text/html",index_html);});
server.on("/style.css", handleCss);
server.on("/Lg1", l1.toggleLight); //ERROR HERE
server.on("/Lg2", l2.toggleLight); //ERROR HERE
server.on("/Door",TDoor);
server.on("/Doorpulse", PDoor);
server.begin();
}
void handleCss(){
server.send_P(200, "text/css", style_css);
}
void loop()
{
server.handleClient();
if(digitalRead(l1.InPin) == LOW){
l1.toggleLight();
delay(1000);
}
if(digitalRead(l2.InPin) == LOW){
l2.toggleLight();
delay(1000);
}
if(digitalRead(Bdoor) == LOW){
Tdoor();
delay(1000);
}
}
void PDoor()
{
digitalWrite(Door, HIGH);
delay(5000);
digitalWrite(Door, LOW);
}
void TDoor(){
digitalWrite(Door,!digitalRead(Door));
String doorstate = digitalRead(Door) ? "Status: Unlocked" : "Status: Locked";
server.send(200,"text/html", doorstate);
}
看来你需要一个lambda函数,和你之前的例子类似。
server.on("/Lg1", [](){ l1.toggleLight(); });