Creating new instance of the class in another class

Hi guys,

A bit of noob question, im new to C and I can't seem to figure on how to construct new instance of the object in my own custom class..

So this example does work, because I implement straight in main file...

#include <TaskScheduler.h>

Task t1(2000, 10, &t1Callback);

void t1Callback() {
    Serial.print("t1: ");
    Serial.println(millis());
}

but I can't seemed to get right syntax to implement it in class... for example:

TestClass.h:

#include <TaskScheduler.h>

#ifndef TESTCLASS_H_
#define TESTCLASS_H_

class TestClass
{
	private:

		Task _task;
};

extern TestClass test;


#endif /* TESTCLASS_H_ */

TestClass.cpp:

#include <TaskScheduler.h>

#include "TestClass.h"

void TestClass::taskCallBack()
{
// do something
	
};


TestClass::TestClass()
{
	
	_task = new Task(1,-1, &TestClass::taskCallBack);
}

this gives compiler error... Could some one direct me on how to do this correctly?

this gives compiler error

Want to clue us in?

I can see that you are going to have a problem, because the callback method must be static.

Thank your for reply...

Want to clue us in?

I'm getting compiler error "error: lvalue required as unary '&' operand" ...

I can see that you are going to have a problem, because the callback method must be static.

But its not static in first example, so why does it need to be static in class?

But its not static in first example, so why does it need to be static in class?

It doesn't have to be static in the first example because the function is not a class method.

	_task = new Task(1,-1, &TestClass::taskCallBack);

TestClass does not appear to have a taskCallback() method.

It doesn't have to be static in the first example because the function is not a class method.

Interesting, good to know :slight_smile:
But when I try to make callback static I get following error:

error: cannot declare member function 'static void TestClass::taskCallback()' to have static linkage [-fpermissive]

TestClass does not appear to have a taskCallback() method

You mean in header file? Yes, I'm sorry this not real example that i'm working on. I wrote this test class straight in comments box... to simplify discussion... in my app I do have it declared in header file.

Do you think you could give me example of the syntax of how I can achieve what I want? Or is syntax correct and i'm doing something else wrong?

to simplify discussion

So, you want to discuss random code that has obvious problems? Or, you you rather discuss YOUR code?

But when I try to make callback static I get following error:

We need to see what you are seeing, not just the errors.

Sorry Paul. I made sample project, so you can check it out.

I hope that helps.

TaskScheduler.zip (18.1 KB)

TestClass.zip (9.86 KB)

You want a function to create new instances of Task?
Or do you want just one Task (named _task, for example) inside the TestClass, so you can use the functions using this _task?

I copied the zip files into the appropriate places. When I try to compile your sketch, I get:

TestClass.cpp: In constructor 'TestClass::TestClass()':
TestClass.cpp:14: error: no matching function for call to 'Task::Task(int, int, void (TestClass::)())'
C:\Users\pjs9486\Documents\Arduino\libraries\TaskScheduler/TaskScheduler.h:77: note: candidates are: Task::Task(long unsigned int, long int, void (
)())
C:\Users\pjs9486\Documents\Arduino\libraries\TaskScheduler/TaskScheduler.h:74: note: Task::Task(const Task&)

Now, the messages may not be very clear, but they stem from the fact that taskCallBack() is not a static method.

When I make taskCallback() a static method

static void taskCallBack();

I get:

TestClass.cpp: In constructor 'TestClass::TestClass()':
TestClass.cpp:14: error: invalid conversion from 'Task*' to 'long unsigned int'
TestClass.cpp:14: error: initializing argument 1 of 'Task::Task(long unsigned int, long int, void (*)())'

which stems from the fact that new returns a pointer, and you are not storing the pointer in a pointer variable.

When I fix that:

Task *_task;

I get:

core.a(main.cpp.o): In function main': C:\Users\pjs9486\Documents\Arduino_105\hardware\arduino\cores\arduino/main.cpp:11: undefined reference to setup'
C:\Users\pjs9486\Documents\Arduino_105\hardware\arduino\cores\arduino/main.cpp:14: undefined reference to `loop'

And that stems from the fact that the .ino file can not have the same name as the source file, since the .ino file will be converted to a cpp file for compiling.

giova014:
You want a function to create new instances of Task?
Or do you want just one Task (named _task, for example) inside the TestClass, so you can use the functions using this _task?

One instance of the task that I can use within TestClass.

PaulS:
I copied the zip files into the appropriate places. When I try to compile your sketch, I get:
Now, the messages may not be very clear, but they stem from the fact that taskCallBack() is not a static method.

When I make taskCallback() a static method

static void taskCallBack();

I get:which stems from the fact that new returns a pointer, and you are not storing the pointer in a pointer variable.

When I fix that:

Task *_task;

I get:And that stems from the fact that the .ino file can not have the same name as the source file, since the .ino file will be converted to a cpp file for compiling.

Thank you Paul for such walkthrough .. But strangely our error messages do not match. First one I got same as what you pasted. But the second I made my callback static I got old error plus "error: cannot declare member function 'static void TestClass::taskCallBack()' to have static linkage [-fpermissive]".

Here is full error:

Compiling 'SuperCool' for 'Arduino Uno'
TestClass.cpp:In constructor 'TestClass::TestClass()'
TestClass.cpp:14:8: error: invalid user-defined conversion from 'Task*' to 'const Task&' [-fpermissive]
  _task = new Task(1,-1, &TestClass*:taskCallBack);
TaskScheduler.h:Task(long unsigned int, long int, void (*)()) <near match>
TaskScheduler.h:no known conversion for argument 1 from 'Task*' to 'long unsigned int'
TestClass.cpp:14:8: error: invalid conversion from 'Task*' to 'long unsigned int' [-fpermissive]
  _task = new Task(1,-1, &TestClass*:taskCallBack);
TaskScheduler.h:77:2: error:   initializing argument 1 of 'Task::Task(long unsigned int, long int, void (*)())' [-fpermissive]
TestClass.cpp:At global scope
TestClass.cpp:17:37: error: cannot declare member function 'static void TestClass::taskCallBack()' to have static linkage [-fpermissive]
 static void TestClass*:taskCallBack()
Error compiling

Then when I add pointer as you mentioned... I left just with following error:

TestClass.cpp:17:37: error: cannot declare member function 'static void TestClass::taskCallBack()' to have static linkage [-fpermissive]

If I then remove the static, but leave pointer, I get following error:

Compiling 'SmartEtch' for 'Arduino Uno'
Binary sketch size: 13 360 bytes (used 41% of a 32 256 byte maximum) (2,15 secs)
Minimum Memory Usage: 1125 bytes (55% of a 2048 byte maximum)
Compiling 'SuperCool' for 'Arduino Uno'
TestClass.cpp:In constructor 'TestClass::TestClass()'
TestClass.cpp:14:49: error: no matching function for call to 'Task::Task(int, int, void (TestClass::*)())'
  _task = new Task(1,-1, &TestClass*:taskCallBack);
TestClass.cpp:candidates are
TaskScheduler.h:Task(long unsigned int, long int, void (*)())
TaskScheduler.h:*)()' to 'void (*)()'
TaskScheduler.h:Task(const Task&)
TaskScheduler.h:candidate expects 1 argument, 3 provided
Error compiling

Sorry for taking so much of your time. I have now put static back on and renamed ino file... So just stuck with that error "error: cannot declare member function 'static void TestClass::taskCallBack()' to have static linkage [-fpermissive]"

Any more ideas?

Test Class After Changes.zip (10.3 KB)

So just stuck with that error "error: cannot declare member function 'static void TestClass::taskCallBack()' to have static linkage [-fpermissive]"

The method needs to be declared static in the header file, but not in the source file.

YES!!!!!

Its compiled. Thank you, thank you, thank you.

Do you mind just explaining me why do we declare in header as static in header and not in source? Is it just specifics of the C language. Its just I usually program in C# and its very different to C.

Also why did I had to specify pointer in class and not in main file?

Do you mind just explaining me why do we declare in header as static in header and not in source?

The implementation of a method needs to have the same signature as the declaration of the method. The static keyword is not part of the declaration. It is more along the lines of a C# decoration. It tells the compiler that the method is a class method, not an instance method.

Hmmm interesting... But I assume that header file plays same role as Inteface in C#. And in C# its actually other way around, you can not declare static method in interface, but you can have it in class itself.

Anyways thanks for your help!