How to create multiple class objects with given Type

I want to create an Object with a type from another class (class Test needs the "type"-class runnable)

#define maxTasks 10 //defines max Number of Tasks you want to handle with one "Category" Task manager

class runnable {
  public:
     void runTask(); //Class needs to be implemented in all Subclasses to get "runnable"
};


class Task {
  private:
    String taskName;
    unsigned long lastTaskCall;
    int callDelay;
    runnable object;

   public:
    Task(){}
   
    Task(String taskName, runnable object, int callDelay = 100, unsigned long lastTaskCall = 0)
    {
        setTaskName(taskName);
        setCallDelay(callDelay);
        setLastTaskCall(lastTaskCall);
        setTaskObject(object)  ;     
    }


    runnable getTaskObject()
    {
      return object;
    }

    void setTaskObject(runnable object)
    {
      this->object = object;
    }
    void setTaskName(String newTaskName)
    {
      taskName = newTaskName;
    }

    void setCallDelay(int newCallDelay)
    {
      callDelay = newCallDelay;
    }

    void setLastTaskCall(unsigned long newLastTaskCall)
    {
      lastTaskCall = newLastTaskCall;
    }

    String getTaskName()
    {
      return taskName;
    }

    int getCallDelay()
    {
      return callDelay;
    }

    unsigned long getLastTaskCall()
    {
      return lastTaskCall;
    }

    /*
     *  helper function
     * 
     * 
     */

     void showObject()
     {
        String test = "hallo";
        Serial.println((String)"-----Object " + this->getTaskName() + "-----");
        Serial.println((String)"Name: " + this->getTaskName());
        Serial.println((String)"callPeriod: " + this->getCallDelay());
        Serial.println((String)"lastCall: " + this->getLastTaskCall());
       
     }
};


class ManageTasks {
  private:
    Task taskList[maxTasks];
    int taskCounter = 0;
    int taskPointer;
   
  public:
//Add new Task to Array (only taskCounter < maxTasks)
    void addTask(String taskName, runnable object, int callDelay = 100, unsigned long lastTaskCall = 0)
    {
      if(taskCounter < maxTasks)
      {
        Task task(taskName, object, callDelay, lastTaskCall);
        taskList[taskCounter] = task;
        taskCounter++;
      }
    }

    void delTask(String taskName)
    {
      for(int pointer = 0; pointer <= taskCounter; pointer++)
      {
        if(taskList[pointer].getTaskName() == taskName)
        {
          for(int i = pointer + 1; i < maxTasks; i++)
          {
            taskList[i-1] = taskList[i];
          }
        }
      }
    }

    void runTask()
    {
      runnable taskObject =  taskList[taskPointer].getTaskObject();
      taskObject.runTask();
      if(taskPointer < maxTasks && taskPointer < taskCounter)
      {
        taskPointer++;
      }
      else
      {
        taskPointer = 0;
      }
    }
   
};


//This class is an example class - represents a later "Task"-function to be called e.g WiFi functionalities - This class must be type runnable but is not...

[NOT WORKING]

class Test : public runnable {

public:
  Test(){}
  void runTask()
  {
    Serial.println("Task1");
  }
};



void setup()
{
  Serial.begin(9600);
  ManageTasks mainTasker = ManageTasks();
  mainTasker.addTask("testTask", new Test(), 150, 260);
}

void loop()
{
//Why is mainTasker not accessible in loop?
 mainTasker.runTask();
}

This is my Sketch -

First Error is the type Problem I think:

no matching function for call to 'ManageTasks::addTask(const char [9], Test*, int, int)'

the second Error is i cant access the "mainTasker object in my "loop" function - but i think this is stuff for another Topic and Google :slight_smile:

Back to my primary question - Why is my "Test"-Class not the correct type - it is a Subclass of runnable or not?
what did i do wrong?

I hope my Question is accurate enough to get answered :slight_smile:

The first error is due to the fact that your class wants a String (upper case S) and you are passing in a c-string (array of char) and the two are not the same. It is best to avoid the String class as it will fragment your limited memory.

You declare mainTasker in your setup() function so when that function exits, that variable goes out of scope and is not longer available. You need to declare that as a global variable if you want to access it in setup() and loop() [and elsewhere]

Even though Java and C++ are both OOP. They are very different. In fact, Java is designed to be everything that C/C++ is not.

You need to forget Java coding pattern and learn C++ from scratch.

addTask() expects an object but you are passing it a pointer.