Page 1 of 2
server.on....
Posted:
Thu Aug 13, 2015 7:40 am
by Molda
Hi
could someone please help me a bit.
I need to pass a class member function to server.on, unfortunately i get na error.
error: no matching function for call to 'ESP8266WebServer::on
This works:
void handleRoot(){}
server.on("/", handleRoot);
This doesn't:
void myClass::handleRoot(){}
void myClass::setup(){
server.on("/", handleRoot);
}
Why does this not work?
Thank you for any help
Re: server.on....
Posted:
Thu Aug 13, 2015 8:49 am
by martinayotte
Maybe you should show the whole code, for example, how do you instantiate your myClass object.
Because you needs to pass your myClass::handleRoot() callback function to server.on() function properly.
Re: server.on....
Posted:
Thu Aug 13, 2015 11:05 am
by Molda
Thanks for your comment.
There isn't much more to it really
myClassTest.ino
Code: Select all#include <ESP8266Wifi.h>
#include <ESP8266WebServer.h>
#include "myClass.h"
void setup(){}
void loop(){}
myClass.h
Code: Select allclass myClass {
public:
void handleRoot();
void setup();
}
myClass.cpp
Code: Select all#include <ESP8266Wifi.h>
#include <ESP8266WebServer.h>
#include "myClass.h"
ESP8266WebServer server(80);
void myClass:: handleRoot(){}
void myClass:: setup(){
handleRoot (); // here it runs OK.
// unfortunately not when passed as argument
// it doesn't compile
server.on("/", handleRoot);
}
If I remove myClass:: from void myClass:: handleRoot(){} it runs ok
I don't instantiate the class in myClassTest.ino
Re: server.on....
Posted:
Thu Aug 13, 2015 11:06 am
by igrr
You need to bind your function call to a specific MyClass instance, unless handleRoot is a static member function.
Pass std::bind(&MyClass::handleRoot, this) instead of &handleRoot.