Reputation: 61
I'm working at learning how to effectively Separate my C++ code for a ESP8266 project and I'm running into some issues with pulling out the webserver functions and objects. I'm using the sample code from this tutorial as my base point.
I feel like I'm understanding the basics of how to build a class and header, but when trying to use another class within my class I'm getting errors...but only in some of the functions.
Here is my main.cpp:
#include <CServer.h>
CServer clientServer(80); //create a CServer, which will create a private WebServer object on init.
void setup(void)
{
clientServer.startRoutes(); //start routes declared in CServer.cpp
clientServer.begin(); // start the server
}
void loop(void){
clientServer.handleClient(); // Listen for HTTP requests from clients
}
This is my CServer.h:
//begin include for class CServer to handle client server functions
#ifndef CSERVER_H
#define CSERVER_H
#include <ESP8266WebServer.h>
//Client server functions are handled by this class.
ESP8266WebServer
class CServer
{
private:
ESP8266WebServer server; //local instance of the webserver object
public:
CServer(int port);
void handleClient();
void handleNotFound();
void handleRoot();
void begin();
void startRoutes();
};
#endif
And finally the CServer.cpp
#include "CServer.h"
CServer::CServer(int port)
{
ESP8266WebServer server(port); //create a server with the passed port
}
void CServer::handleClient()
{
server.handleClient();
};
void CServer::handleRoot()
{
server.send(200, "text/plain", "Hello world!"); // Send HTTP status 200 (Ok) and send some text to the browser/client
}
void CServer::handleNotFound()
{
server.send(404, "text/plain", "404: Not found"); // Send HTTP status 404 (Not Found) when there's no handler for the URI in the request
}
void CServer::begin()
{
server.begin();
}
void CServer::startRoutes()
{
server.on("/", handleRoot);
server.onNotFound(handleNotFound);
}
I'm getting the error:
no matching function for call to 'esp8266webserver::ESP8266WebServerTemplate::on(const char [2], )'
for both the handleRoot, and handleNotFound functions i'm trying to pass to "server.on" and "server.onNotFound" at the very end of the .cpp file.
I'm looking to do this kind of thing with many different libraries and such to build a device, and would really like to know how to effectively clean up my code with this strategy, so understanding the right way is critical.
Upvotes: 0
Views: 1092
Reputation: 22152
You are trying to pass a non-static member function to server.on("/", handleRoot);
which expects a callable of signature void()
(i.e. a callable that can be called without any arguments and returning void
).
A non-static member function however is not a function callable without any argument. It takes at least one argument, the implicit this
pointer to the class instance.
If you want the handler to call handleRoot
on the current class instance on which startRoutes
was called, then use a lambda to call the member and capture this
:
server.on("/", [this]{ handleRoot(); });
As @Peter mentioned in the question comments, you are also not initializing the server
member properly. Instead you are creating a local instance of ESP8266WebServer
, also called server
, but which is local to the constructor and destroyed when the constructor leaves. This instance has no relation to the class member instance, except that they share type and name.
If you want to initialize the server
member with port
as argument, you can do that in the member initializer list of the constructor:
CServer::CServer(int port) : server(port)
{
}
Upvotes: 4