1

I have a class named timeOut dealing with timeout tasks.

I'm to write a sketch, common for Sonoff basic and Sonoff Dual, meaning that I may have 1 task for Basic and 2 tasks for Dual.

Declaring instances looks like:

timeOUT timeOut_SW0("SW0",TIMEOUT_SW0);
timeOUT timeOut_SW0("SW1",TIMEOUT_SW1);

for code simplicity I'd rather create an array of references and call it using a for loop:

timeOUT TO[]={timeOut_SW0,timeOut_SW1};

is it the right way to call it as a reference ?

asked Jul 17, 2019 at 19:10
3
  • 2
    a few questions back I answered a similar question with example arduino.stackexchange.com/questions/67170/… Commented Jul 17, 2019 at 19:42
  • is this a school assignment? Commented Jul 17, 2019 at 20:45
  • @jsotola No it is not Commented Jul 18, 2019 at 3:10

1 Answer 1

3

You can't. The C++ language doesn't support arrays of references. You have the choice to either create an array of objects:

timeOUT TO[] = {timeOUT("SW0",TIMEOUT_SW0), timeOUT("SW1",TIMEOUT_SW1);}

or an array of pointers:

timeOUT *TO[] = { &timeOut_SW0, &timeOut_SW1 };
answered Jul 17, 2019 at 19:29
3
  • timeout_Switch:243:34: error: request for member 'remain' in 'TO[i]', which is of pointer type 'timeOUT*' (maybe you meant to use '->' ?) if(TO[i].remain()>0) { ^ Commented Jul 17, 2019 at 19:44
  • I upvoted this answer, and going to delete mine since it's wrong. Commented Jul 17, 2019 at 19:44
  • I chose 2nd option you offered Commented Jul 17, 2019 at 19:46

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.