Error Instancing PubSubClient In Library

Hello,

I've been trying out the PubSubClient library with my ESP to communicate to MQTT brokers, and have had no issues from uploading a sketch similar to this example from the github pubsubclient/mqtt_auth.ino at master · knolleary/pubsubclient · GitHub .

I wanted to take the sketch and turn it into a library, but ran into an issue trying to compile which I don't really understand.

In the .h file, I'm creating an instance of the PubSubClient library with the following line.

PubSubClient client(_mqttServer, _mqttPort, Callback, wifiClient);

But when I try to compile I get errors for each of the parameters about how they are "not a type".

This is probably a basic question, but I've never ran into this before and am unsure what I'm doing wrong.

Can anyone point me in the right direction?

The .h file:

#ifndef MQTT_h
#define MQTT_h

#include "Arduino.h"
#include <ESP8266WiFi.h>
#include <PubSubClient.h>

class MQTT
{
	public:
		MQTT();	
		void ConnectToWiFi();
		void Loop();

	private:
		void Callback(char* topic, byte* payload, unsigned int length); 
		void ConnectToBroker();
		
		const char* _ssid = "...";
		const char* _password =  "...";

		const char* _mqttServer = "...";
		const int _mqttPort = 12798;
		const char* _mqttUser = "...";
		const char* _mqttPassword = "...";
		
		byte mac[6];
		
		WiFiClient wifiClient;
		PubSubClient client(_mqttServer, _mqttPort, Callback, wifiClient);
};
#endif

The .cpp file:

#include "Arduino.h"
#include "MQTT.h"

MQTT::MQTT()
{
}

void MQTT::ConnectToWiFi()
{
	Serial.begin(38400);
	delay(500);
	
    WiFi.begin(ssid, password);
    Serial.println("Connecting to WiFi");
    while (WiFi.status() != WL_CONNECTED) 
    {
        delay(500);
        Serial.print(".");                   
    }
    Serial.println("\nConnected to the WiFi network.");
    WiFi.macAddress(mac);
    ConnectToBroker();
}

void MQTT::Callback(char* topic, byte* payload, unsigned int length)
{
	Serial.print("Message arrived in topic: ");                  
    Serial.println(topic);  
}

void MQTT::ConnectToBroker()
{
	if (wifiClient.connected())            
        wifiClient.stop();

    while (!client.connected())                                      
    {
        Serial.println("Connecting to MQTT broker...");
        if (client.connect("ESP8266Client", _mqttUser, _mqttPassword ))
            Serial.println(" Connected.");            
        else 
        {
            Serial.println("Failed with state " + client.state());
            delay(2000);
        }
    }

    client.subscribe("testTopic");
}

void MQTT::Loop()
{
	client.loop();
}

Here's the arduino sketch, though it works fine so it's probably not helpful.

#include <ESP8266WiFi.h>
#include <PubSubClient.h>

const char* ssid = "...";
const char* password =  "...";

const char* mqttServer = "...";
const int mqttPort = 12798;
const char* mqttUser = "...";
const char* mqttPassword = "...";

byte mac[6];

void Callback(char* topic, byte* payload, unsigned int length); 

WiFiClient wifiClient;
PubSubClient client(mqttServer, mqttPort, Callback, wifiClient);

void Callback(char* topic, byte* payload, unsigned int length) 
{
    Serial.print("Message arrived in topic: ");                  
    Serial.println(topic);  
}

void ConnectToBroker()
{
    if (wifiClient.connected())            
        wifiClient.stop();

    while (!client.connected())                                      
    {
        Serial.println("Connecting to MQTT broker...");
        if (client.connect("ESP8266Client", mqttUser, mqttPassword ))
            Serial.println(" Connected.");            
        else 
        {
            Serial.println("Failed with state " + client.state());
            delay(2000);
        }
    }

    client.subscribe("testTopic");
}

void setup()
{
    Serial.begin(38400);

    WiFi.begin(ssid, password);
    Serial.println("Connecting to WiFi");
    while (WiFi.status() != WL_CONNECTED) 
    {
        delay(500);
        Serial.print(".");                   
    }
    Serial.println("\nConnected to the WiFi network.");
    WiFi.macAddress(mac);
    ConnectToBroker();
}

void loop()
{
  client.loop();
}

I stumbled upon this which seems the describe the same thing I was experiencing more or less.

I still don't understand what the problem is which prevented the PubSubClient from being instantiated in the header file, but I'm guessing it had something to do with the fact that arguments were being referenced . Moving the WiFiClient and PubSubClient instances to the .cpp file, then setting the callback the way shown in the url solved the issue for me.

Hopefully that may be useful for anyone else who may find themselves in a similar situation.