Short answer:
Change helloCmd()
to take a WebServer_My2&
instead of a WebServer_My2KNJ&
.
Long answer:
It looks like the problem stems from the Command
function pointer typedef in your WebServer_My2.h
file. It's presumably declared as follows (line ~169):
typedef void Command(WebServer_My2 &server, ConnectionType type,
char *url_tail, bool tail_complete);
That means it's a pointer to a function which accepts a reference to a WebServer_My2
object as its first parameter (followed by some others). There are some methods, such as setDefaultCommand()
, which accept function pointers of this exact type. C++ is strict here. You can't substitute another function pointer which accepts reference parameters of a derived type.
I know you said you didn't modify helloCmd
, but based on the error message, I have to assume you actually changed the parameter list to this:
void helloCmd(WebServer_My2KNJ &server, WebServer_My2::ConnectionType type, char *, bool)
The problem therefore comes when the code tries to do these lines:
webserver.setDefaultCommand(&helloCmd);
webserver.addCommand("index.html", &helloCmd);
The setDefaultCommand()
and addCommand()
methods are expecting a function pointer which takes a WebServer_My2
reference as its first parameter. However, helloCmd()
actually takes a WebServer_My2KNJ
reference. That won't work because the types are different (even though one is derived from the other).
A quick solution is to change helloCmd()
as I suggested in the short answer above. That way it should at least be able to compile and run the example. If you want to use the extended functionality that you added to your subclass, then you can do this in the function:
WebServer_My2KNJ * myServer = dynamic_cast<WebServer_My2KNJ*>(&server);
if (myServer) {
// myServer is safe to use here
}
You can then use the myServer
pointer to access all your added webserver functionality. Just remember that it's a pointer though, so you need to use the ->
operator instead of the .
operator to access members.
The dynamic_cast
converts down the inheritance hierarchy if possible. It returns a null pointer (0) if the conversion doesn't work, which is why the if ()
block is necessary.