Platformino can not see class instance in other heather files

I create a project for measuring the sensor valve and sending it over wifi with WebSocket connection there a couple of header files in this project.
in my_wifi.cpp file I declare 2 instances of the class

#include sensor.h
.
.
.
AsyncWebServer server(80);
AsyncWebSocket WS("/ws");

by when I call these instances in my sensor.cpp file, it throws the error
src/sensor.cpp:89:13: error: 'WS' was not declared in this scope

#include "my_wifi.h"
.
.
.

if(WIFI_ENABALE)   WS.text(client,data);

```

Where did you place the

#include <AsyncWebSocket.h>
extern AsyncWebSocket WS;

code that allows the global variable to be seen by other files, if they include it?

tnx @maxgerhardt but, I’ve tried it and let to more error

In file included from src/main.cpp:4:
include/wifi_c.h:10:19: error: variable 'AsyncWebSocket WS' has initializer but incomplete type
  AsyncWebSocket WS("/ws");
                   ^
In file included from include/tacho.h:8,
                 from src/tacho.cpp:1:
include/wifi_c.h:10:19: error: variable 'AsyncWebSocket WS' has initializer but incomplete type
  AsyncWebSocket WS("/ws");

src/tacho.cpp:4:25: warning: 'WS' initialized and declared 'extern'
 extern AsyncWebSocket WS("/ws");
                         ^
src/wifi_c.cpp:19:18: error: redefinition of 'AsyncWebSocket WS'
 AsyncWebSocket WS("/ws");

You should never create a global variable in a header file, just declare it as extern with no constructor arguments, like I did above. The

  AsyncWebSocket WS("/ws");

variable creation goes into the .cpp file.

1 Like