ESP8266 wifi class, initialize an webserver object within a class

Hello together!

I know this is more a programming question than a arduino specific issue, but I've been through some C++ tutorials and I can't find a solution the compiler finds acceptable. However I hope this is an easy thing for you programming pros here :slight_smile: Actaually I think I know what I want, but this seems to be a syntax problem...

Well, I try to create a class for all my wifi and server function on an ESP8266 and within this class I want to create an object for the webserver. Usually I could create a global object called "server" and webSocket and use it, which works fine, very simple thing:

#include <ESP8266WiFi.h>        // Include the Wi-Fi library
#include <ESP8266WebServer.h>   // Include the WebServer library
#include <FS.h>                 // Include the SPIFFS library
#include <WebSocketsServer.h>   

ESP8266WebServer server(80);       // Create a webserver object that listens for HTTP request on port 80
WebSocketsServer webSocket(81);    // create a websocket server on port 81

Now I would like to encapsulate this all a bit and create my server objects within a class:

class Wifi{
  private:
    ESP8266WebServer server(80);       // Create a webserver object that listens for HTTP request on port 80
    WebSocketsServer webSocket(81);    // create a websocket server on port 81
  public:
    Wifi(void);            //Constructor
};

However the compiler doesn't like this at all, it says "Wifi.h:27:29: error: expected identifier before numeric constant" Obviously I am not allowed to initialize my objects in the declaration. Actually this would work when the objects don't need parameters, but these ones do! :frowning:

So I try to move the initialization into the constructor:

class Wifi{
  private:
    ESP8266WebServer server;       // Create a webserver object that listens for HTTP request on port 80
    WebSocketsServer webSocket;    // create a websocket server on port 81
  public:
   Wifi(void);            //Constructor
};


Wifi::Wifi(void){
  ESP8266WebServer server(80);       // Create a webserver object that listens for HTTP request on port 80
  WebSocketsServer webSocket(81);    // create a websocket server on port 81  
}

I also tried:

Wifi::Wifi(void){
  server(80);       // Create a webserver object that listens for HTTP request on port 80
  webSocket(81);    // create a websocket server on port 81  
}

Compiler says no!

So to wrap up my question:
How do I declare my objects, that need parameters for creation, in the class definition and call their constructors in the constructor of my class later?

Sorry again to annoy you with such a stupid question, as I said I can't find anything via google about this, probably beacuse I don't know the right "buzzword".

Thank you!

Best regards
Daniel

The correct syntax is:

.h

class Wifi{
  private:
    ESP8266WebServer server;       // Create a webserver object that listens for HTTP request on port 80
    WebSocketsServer webSocket;    // create a websocket server on port 81
  public:
    Wifi(void);            //Constructor
};

.cpp

Wifi::Wifi() : 
   server(80),
   webSocket(81)
{
}

Hello!

Thanks for the answer, but I tried this and it says:

Wifi.h:48:12: error: no match for call to '(ESP8266WebServer) (int)'

server(80), webSocket(81); // Create a webserver object that listens for HTTP request on port 80

^

Wifi.h:48:27: error: no match for call to '(WebSocketsServer) (int)'

server(80), webSocket(81); // Create a webserver object that listens for HTTP request on port 80

:frowning:

Hey!

What actually works is not declaring the objects at all in the class definition and just create them in the constructor, but this seems to me a bit dirty to me...

Best regards
Daniel

Pay attention to my reply. They are not function calls, they are list initializers.
There is a : after Wifi::Wifi().
There is no ; before the {

Ahhh! Thank you a lot! I came across this topic in the C++ tutorial, but I never really got the point about the list initializers! But now! :slight_smile:

Best regards
Daniel