Initialize a global pin after declaration (Constructor error: "Constructor for [class] must explicitly initialize the member [public variable] which does not have a default constructor")

So, I’m somewhat new to mbed and wanted to try using a class with global pins.
The project is about a traffic-light-system with multiple traffic lights. So, my thought was to create a class “TrafficLight” and create multiple objects from that class.
The lights are obviously pins, but I want to declare different pins for different objects, but I have the error “Constructor for ‘TrafficLight’ must explicitly initialize the member ‘_redLight’ which does not have a default constructor”

My class:

class TrafficLight{
    public:
        DigitalOut _redLight;
        DigitalOut _yellowLight;
        DigitalOut _greenLight;

        TrafficLight(PinName red, PinName yellow, PinName green){
            _redLight = DigitalOut(red);
            _yellowLight = DigitalOut(yellow);
            _greenLight = DigitalOut(green);
        }
};

And my idea is to say like:

TrafficLight trafficLight(ARDUINO_UNO_D10, ARDUINO_UNO_D9, ARDUINO_UNO_D8);
...
main(){
 trafficLight._redLight = 1;
}

but the Constructor gives the error as before mentioned.
Any ideas, how to correct this?

Hello,

this is not about Mbed knowledge but about C++ knowledge. It has to be

class TrafficLight{
   public:
        DigitalOut _redLight;
        DigitalOut _yellowLight;
        DigitalOut _greenLight;

        TrafficLight(PinName red, PinName yellow, PinName green):_redLight(red), _yellowLight(yellow), _greenLight(green){  }
};

or with pointers

class TrafficLight{
   public:
        DigitalOut* _redLight;
        DigitalOut* _yellowLight;
        DigitalOut* _greenLight;

        TrafficLight(PinName red, PinName yellow, PinName green){
            _redLight = new DigitalOut(red);
            _yellowLight = new DigitalOut(yellow);
            _greenLight = new DigitalOut(green);
        }
};

BR, Jan

Thanks. It’s at least not giving me errors in this part of the code anymore :+1: