Class initializing won't work with dependencies

Hey^^,

i’ve some problems expanding a class of my project…

in General my project works fine (it compiles without error) but now i want to include into my Logging class a mqtt functionality (sending logs via mqtt) so i try to include my wifi and mqtt libary (both of these classes include the logging class so both classes depend on the Logging class) - i’ve looked for hours on goolge and many different forums but it seems to be another problem…

Now i think i can’t handle this problem without any help… so i hope that someone here can help me…

The following link is the Compiler Output:

Only the Error of the compiler:

My Project is: https://github.com/j54j6/ESPBase/tree/dev_linearWeb
Branch: dev_linearWeb

Logger Libary (only problems if i want to extend with WifiManager and MQTTManager Libary - normally it works)
https://github.com/j54j6/ESPBase/tree/dev_linearWeb/lib/logging/src

The Other Libaries are here: https://github.com/j54j6/ESPBase/tree/dev_linearWeb/lib

I hope that someone can help me…

thank you all for help

best regards

There are a lot of circular dependencies in that project, leading to an around 8600 lines long library dependency graph. The project is very strongly coupled. For example, logger.h includes mqtthandler.h which includes moduleState.h which includes logger.h, … Everything depends on everything. I’m seeing the usage of forward declarations to break the include chain but then that file is included anyways.

Your project builds if I break up the circular dependency in logger.h to 3 other submodules by removing the lines

#include "mqttHandler.h"
#include "wifiManager.h"
#include "moduleState.h"

since you’ve forward-declared these classes (and don’t use any specific functions in them or constructors), this works out nicely.

This also shortens the dependency graph to ~2200 lines.

Try and keep module coupling to an absolute minimium. If the logger needs to log via MQTT or WiFi or to a file, the MQTT or whatever logic should be stand-alone and not need the logger – that leads to circularily otherwise. Strict seperation should be applied here.

Also your platformio.ini isn’t quite right – it has the lib_deps declarations in old libraryname-only style. For me e.g. it picked up Arduino Json 5.x with which it didn’t build and I explicitly needed version 6.x. Also WiFiClient is an invalid dependency since it’s part of the ESP8266WiFi library which is right written as a dependency right above it. It should look like

[env:nodemcu]
platform = espressif8266
board = nodemcu
framework = arduino
monitor_speed = 921600
upload_speed = 921600
lib_ldf_mode = chain+
lib_deps = 
    bblanchon/ArduinoJson @ ^6.17.2
    ESP8266WiFi
    ESP8266WebServer
    ESP8266mDNS
    DNSServer
    knolleary/PubSubClient
    arduino-libraries/NTPClient
;    gioblu/PJON @ ^12.1
    adafruit/Adafruit Unified Sensor @ ^1.1.4
    adafruit/DHT sensor library @ ^1.4.1

I didn’t touch knolleary/PubSubClient since I don’t know what version you want to use exactly.

Builds after changes.

grafik

Thanks for your help…
In this Case it works fine for me but i want to use the functionality of these Modules in my Logger Class…

If i use your Solution it compiles fine but i can’t use the mqtt function because the compiler don’t know what “mqtt” is…
I added the part in the SysloggerClass file (line 205 - 213 - what should I need to do to get this working?).
Link to the File: https://github.com/j54j6/ESPBase/blob/dev_linearWeb/lib/logging/src/logger.cpp

Error: https://pastebin.com/hah2wZJB

Is that what i want to do possible that I can use the SysLogger Class inside the MQTT Class but also use the MQTT Class inside the SysLogger Class (and later maybe the same thing with other classes…)

Oh and can you explain me a “best practice” for the following statement in your last answer^^ :

Try and keep module coupling to an absolute minimium. If the logger needs to log via MQTT or WiFi or to a file, the MQTT or whatever logic should be stand-alone and not need the logger

Because i want to log every moule to see errors or information for debuging and i thougt it is “goog-practice” if i write a own class for logging and include it in any sub Module of my project so i don’t need to write a logger for each class :^)… what is the “best practice” here?

Thank you so much for your help.
I appreciate you :).

Since we’ve removed the three includes from the .h file to get a valid non-circular declaration of all functions, the .cpp file will need to include those files now to access the real underlying functionality.

After

you should try and add the include lines back.