From bd27edbb8d5df3293abb745110d4d3e3019ef6f6 Mon Sep 17 00:00:00 2001 From: Aminho09 Date: Wed, 29 Jan 2025 21:26:57 +0330 Subject: [PATCH] Resolve constructor mismatch for SCTimerService initialization on trafficlight-qt-cpp Previously, SCTimerService was instantiated using a shared_ptr, but its constructor only accepted a QObject*. This caused a compilation error due to a missing matching constructor. - Updated SCTimerService to accept shared_ptr in its constructor. - Ensured proper QObject hierarchy by passing the raw pointer from shared_ptr. - Fixed QObject::connect calls by using `.get()` on shared_ptr. - Prevented potential bad_weak_ptr errors by ensuring TrafficLightStateMachine is managed by shared_ptr. --- .../implementation/main.cpp | 21 ++++++++++--------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/itemis.create.examples.trafficlight.cpp.qt/implementation/main.cpp b/itemis.create.examples.trafficlight.cpp.qt/implementation/main.cpp index d1937a0e..7994a12a 100644 --- a/itemis.create.examples.trafficlight.cpp.qt/implementation/main.cpp +++ b/itemis.create.examples.trafficlight.cpp.qt/implementation/main.cpp @@ -19,22 +19,23 @@ int main(int argc, char *argv[]) return -1; - TrafficLightStateMachine *machine = new TrafficLightStateMachine(nullptr); - sc::qt::SCTimerService *timerService = new sc::qt::SCTimerService(machine); + std::shared_ptr machine = std::make_shared(nullptr); + std::shared_ptr timerService = std::make_shared(nullptr); machine->setTimerService(timerService); + QObject *root = engine.rootObjects().at(0); QObject *control = root->findChild(QString("control")); - QObject::connect(control, SIGNAL(pause()), machine, SLOT(standby())); - QObject::connect(control, SIGNAL(play()), machine, SLOT(operate())); + QObject::connect(control, SIGNAL(pause()), machine.get(), SLOT(standby())); + QObject::connect(control, SIGNAL(play()), machine.get(), SLOT(operate())); QObject *trafficLight = root->findChild(QString("trafficLight")); - QObject::connect(machine, SIGNAL(redOn()), trafficLight, SLOT(redOn())); - QObject::connect(machine, SIGNAL(redOff()), trafficLight, SLOT(redOff())); - QObject::connect(machine, SIGNAL(yellowOn()), trafficLight, SLOT(yellowOn())); - QObject::connect(machine, SIGNAL(yellowOff()), trafficLight, SLOT(yellowOff())); - QObject::connect(machine, SIGNAL(greenOn()), trafficLight, SLOT(greenOn())); - QObject::connect(machine, SIGNAL(greenOff()), trafficLight, SLOT(greenOff())); + QObject::connect(machine.get(), SIGNAL(redOn()), trafficLight, SLOT(redOn())); + QObject::connect(machine.get(), SIGNAL(redOff()), trafficLight, SLOT(redOff())); + QObject::connect(machine.get(), SIGNAL(yellowOn()), trafficLight, SLOT(yellowOn())); + QObject::connect(machine.get(), SIGNAL(yellowOff()), trafficLight, SLOT(yellowOff())); + QObject::connect(machine.get(), SIGNAL(greenOn()), trafficLight, SLOT(greenOn())); + QObject::connect(machine.get(), SIGNAL(greenOff()), trafficLight, SLOT(greenOff())); machine->enter();