FSMBuilder::createStatesFromJson(prTool1, doc); std::cout <<" " << std::endl;…
- User
FSMBuilder::createStatesFromJson(prTool1, doc); std::cout <<" " << std::endl;
prTool1->SetInitialState("StartToolState"); std::cout << "Initial Active State : " << (prTool1->GetActiveStatus() ? prTool1->GetActiveStatus()->StateName : "None") << std::endl; Event* event1 = new Event(); event1->getdata("CompleteEvent", "ChangeState", "WaitForAccept"); prTool1->InEvent(event1); std::cout << "Active State after CompleteEvent: " << (prTool1->GetActiveStatus() ? prTool1->GetActiveStatus()->StateName : "None") << std::endl; delete event1; delete prTool1; return 0;what to pass as argument in Event
Severity Code Description Project File Line Suppression State Details Error C2512 'Event::Event': no appropriate default constructor available FsmImplementation C:\Users\1038586\Downloads\FsmImplementation11142024\FsmImplementation\FsmImplementation\FSMBase.cpp 70
- Assistant
The error message you're encountering indicates that the
Eventclass does not have a default constructor, meaning you cannot create an instance of theEventclass without passing the required parameters to its constructor.Here’s how to fix this issue:
-
Check the
Eventclass constructor: Look at how theEventclass is defined to understand what parameters it requires. For example, the declaration of the class might look something like this:class Event { public: Event(const std::string& eventName, const std::string& eventType, const std::string& eventTarget); // Other members... };In this case, you would need to pass three arguments of type
std::stringfor the event name, event type, and event target. -
Instantiate the
Eventobject correctly: Given the requirements outlined above, you should create an instance ofEventby providing the necessary arguments. Instead of creating anEventusing the default constructor and then populating it with data, you should call the constructor directly, like so:Event* event1 = new Event("CompleteEvent", "ChangeState", "WaitForAccept"); -
Updated code: Here's how your code snippet will look after making this adjustment:
FSMBuilder::createStatesFromJson(prTool1, doc); std::cout << " " << std::endl; prTool1->SetInitialState("StartToolState"); std::cout << "Initial Active State : " << (prTool1->GetActiveStatus() ? prTool1->GetActiveStatus()->StateName : "None") << std::endl; // Create Event instance using the constructor Event* event1 = new Event("CompleteEvent", "ChangeState", "WaitForAccept"); prTool1->InEvent(event1); std::cout << "Active State after CompleteEvent: " << (prTool1->GetActiveStatus() ? prTool1->GetActiveStatus()->StateName : "None") << std::endl; delete event1; delete prTool1; return 0; -
Ensure memory management: Since you are allocating memory dynamically using
new, always ensure you are matching eachnewwithdeleteto avoid memory leaks. Your current code does this correctly by deleting both theEventandprTool1objects. -
Consider using smart pointers: If you're using C++11 or later, consider using smart pointers (
std::unique_ptrorstd::shared_ptr) to handle memory automatically. For example, instead of raw pointers, you could write:std::unique_ptr<Event> event1 = std::make_unique<Event>("CompleteEvent", "ChangeState", "WaitForAccept"); prTool1->InEvent(event1.release()); // Release ownership to prTool1
This way, memory management becomes less error-prone.
-