Instantiating an object in a class with parameters

I'm trying to make a telnet server class to be used in multitasking, while I'm unable to instantiate ESPTelnet object with port parameter passed to constructor, while the same syntax works in the main file without class

ESPTelnet telnet(PORT); GIVES ERROR 'PORT' is not a type

class telnetTask : public Task {

 public:
    const int PORT = 23;


  protected:
    ESPTelnet telnet (PORT);  //------------------ GIVES ERROR: 'PORT' is not a type

what should be the correct syntax to initiate ESPTelnet object in a class

Seems to me you haven't put up enough to have any idea what could be wrong. Lets see your class. And why is this call protected? Who you protecting it from?

-jim lee

Use an Initializer List. See case #3.

jimLee:
Seems to me you haven’t put up enough to have any idea what could be wrong. Lets see your class. And why is this call protected? Who you protecting it from?

-jim lee

well, I just used to do this as an OOP concept, while changing this to private or public didn’t do any change
If I didn’t pass PORT parameter to the function, it gives another error in calling object funcions “No Matching function for call to 'ESPTelnet::onConnect”

#include "WiFiManager.h"
#include "ESPTelnet.h"
#include <Arduino.h>
#include <Scheduler.h>
#define SERIAL_SPEED    9600
#define PORTAL_TIMEOUT  10 * 60 // seconds
#define AP_NAME         "MY CAPTIVE PORTAL"
#define AP_PASSWORD     ""

class telnetTask : public Task {

  private:
    WiFiManager wifiManager;
    ESPTelnet telnet;

    IPAddress ip_addr;
    /* ------------------------------------------------- */
    void connectToWifi() {
      // wifiManager.resetSettings();  // this will delete all credentials
      wifiManager.setDebugOutput(false);
      wifiManager.setConfigPortalTimeout(PORTAL_TIMEOUT);
      wifiManager.setAPCallback([] (WiFiManager * myWiFiManager) {
        Serial.println("- No known wifi found");
        Serial.print("- Starting AP: ");
        Serial.println(myWiFiManager->getConfigPortalSSID());
        Serial.println(WiFi.softAPIP());
      });
      // enable autoconnect
      if (!(AP_PASSWORD == "" ?
            wifiManager.autoConnect(AP_NAME) :
            wifiManager.autoConnect(AP_NAME, AP_PASSWORD))
         ) {
        Serial.println("- Failed to connect and hit timeout");
        ESP.reset();
        delay(1000);
        // known wifi found & connected
      }  else {
        ip_addr = WiFi.localIP();
        String mac_addr = WiFi.macAddress();
        String wifi = WiFi.SSID();

        Serial.println("- WiFi: " + wifi);
        Serial.println("- IP: " + ip_addr.toString());
      }
    }
    /* ------------------------------------------------- */
    void setupTelnet() {
      if (telnet.begin()) {
        telnet.onConnect(onTelnetConnect);
        telnet.onConnectionAttempt(onTelnetConnectionAttempt);
        telnet.onReconnect(onTelnetReconnect);
        telnet.onDisconnect(onTelnetDisconnect);
        Serial.println("- Telnet is running. Connect to Telnet using:");
        Serial.println("telnet " + ip_addr.toString());

        // could not create server
      } else {
        Serial.println("- Failed to create Telnet server. Is device connected to WiFi?");
        delay(10000);
        ESP.reset();
        delay(1000);
      }
    }
    /* ------------------------------------------------- */
    // (optional) callback functions for server
    void onTelnetConnect() {
      Serial.print("- Telnet: ");
      Serial.print(telnet.getIP());
      Serial.println(" connected");
      telnet.println("\nWelcome " + telnet.getIP());
      telnet.println("(Use ^] + q  to disconnect.)");
    }
    void onTelnetDisconnect() {
      Serial.print("- Telnet: ");
      Serial.print(telnet.getIP());
      Serial.println(" disconnected");
    }
    void onTelnetReconnect() {
      Serial.print("- Telnet: ");
      Serial.print(telnet.getIP());
      Serial.println(" reconnected");
    }
    void onTelnetConnectionAttempt() {
      Serial.print("- Telnet: ");
      Serial.print(telnet.getLastAttemptIP());
      Serial.println(" tried to connected");
    }
    /* ------------------------------------------------- */
    void setup() {
      // setup serial output
      Serial.begin(SERIAL_SPEED);
      while (!Serial) {
        delay(10);
      }
      Serial.println("\n\nTelnet Test");
      connectToWifi();
      setupTelnet();
      Serial.println();
    }
    /* ------------------------------------------------- */
    int counter = 0;

    void loop() {
      telnet.loop();
      telnet << ".";            // same as: telnet.print(".");
      if (counter++ % 10 == 0) telnet.println();
      delay(1000);
    }
};

gfvalvo:
Use an Initializer List. See case #3.

I tried to understand how to Apply it to my code, but I couldn't get it done, any TIP

This compiles without error or warning:

class ESPTelnet
{
  public:
    ESPTelnet(int port);
};


class telnetTask
{
  public:
    telnetTask() : telnet(telnetTask::PORT) {}


    const int PORT = 23;


  protected:
    ESPTelnet telnet;  //------------------ GIVES ERROR: 'PORT' is not a type
};


void setup() {}
void loop() {}

johnwasser:
This compiles without error or warning:

class ESPTelnet

{
  public:
    ESPTelnet(int port);
};

class telnetTask
{
  public:
    telnetTask() : telnet(telnetTask::PORT) {}

const int PORT = 23;

protected:
    ESPTelnet telnet;  //------------------ GIVES ERROR: 'PORT' is not a type
};

void setup() {}
void loop() {}

Works perfectly, Thanks, you made my day