2

I'm using several TM1637 displays to create timers, so I decided to make a Timer class. Every time I initialize an instance of the Timer class, I want it to create an instance of the TM1637Display class. So I started out with this, but the display object can't be accessed from within updateDisplay.

class Timer {
public:
    Timer(uint8_t CLK, uint8_t DIO) {
        TM1637Display display(CLK, DIO);
        ...
    }
    void updateDisplay() {
        ...
        display.setSegments(displayData);
    }
}

And if I do this I get the compile time error In constructor 'Timer::Timer(uint8_t, uint8_t)': src\main.cpp:16:37: error: no matching function for call to 'TM1637Display::TM1637Display()'

    TM1637Display display;
    Timer(uint8_t CLK, uint8_t DIO) {
        display(CLK, DIO);
        ...
    }

What should I try? Maybe make an empty pointer before the display instance is created with a public scope, assign the pointer to the display instance, then hand the updateDisplay function the pointer? That seems really convoluted.

1 Answer 1

4

As it's now in the first example, it's local variable in Timer constructor.

In the second one you have to use member initializer list in constructor as there is no default constructor in your display class.

class Timer {
public:
    Timer(uint8_t CLK, uint8_t DIO)
    : display(CLK, DIO) // constructor's initializer list 
    {
        ...
    }
    void updateDisplay() {
        ...
        display.setSegments(displayData);
    }

    // member variable:
    TM1637Display display;
}
1
  • Thank you! I had never heard of initializer lists before.
    – ahalekelly
    Apr 20, 2018 at 12:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.