Untitled conversation
- User
{ "PrimitiveTool": { "State": [ { "Name": "StartToolState", "EntryMethodName": "EnterStartTool",
code - Assistant
- User
- Assistant
- User
- Assistant
{ "PrimitiveTool": { "State": [ { "Name": "StartToolState", "EntryMethodName": "EnterStartTool",
"Event": {
"Name": "CompleteEvent",
"ActionType": "ChangeState",
"ActionParam": "WaitForAccept"
}
},
{
"Name": "WaitForAccept",
"EntryMethodName": "EnterWaitForAcceptState",
"Event": [
{
"Name": "MotionEvent",
"ActionType": "InvokeMethod",
"ActionParam": "MoveToolPoints"
},
{
"Name": "AcceptEvent",
"ActionType": "ChangeState",
"ActionParam": "AcceptedState"
},
{
"Name": "ClickEvent",
"ActionType": "ChangeState",
"ActionParam": "AcceptedState"
},
{
"Name": "CompleteEvent",
"ActionType": "ChangeState",
"ActionParam": "AcceptedState"
}
]
},
{
"Name": "StartManualEPrimeToolState",
"EntryMethodName": "EnterStartManualEPrimeTool"
},
{
"Name": "ManualSVelPointState",
"EntryMethodName": "EnterManualSVelPoint",
"Event": [
{
"Name": "MotionEvent",
"ActionType": "InvokeMethod",
"ActionParam": "MoveSVelPoint"
},
{
"Name": "ClickEvent",
"ActionType": "InvokeMethod",
"ActionParam": "EnterCompleteManualSVelPoint"
},
{
"Name": "CompleteEvent",
"ActionType": "ChangeState",
"ActionParam": "AcceptedState"
}
]
},
{
"Name": "ManualEVelPointState",
"EntryMethodName": "EnterManualEVelPoint",
"Event": [
{
"Name": "MotionEvent",
"ActionType": "InvokeMethod",
"ActionParam": "MoveEVelPoint"
},
{
"Name": "ClickEvent",
"ActionType": "InvokeMethod",
"ActionParam": "EnterCompleteManualEVelPoint"
},
{
"Name": "CompleteEvent",
"ActionType": "ChangeState",
"ActionParam": "AcceptedState"
}
]
},
{
"Name": "ManualAVelPointState",
"EntryMethodName": "EnterManualAVelPoint",
"Event": [
{
"Name": "MotionEvent",
"ActionType": "InvokeMethod",
"ActionParam": "MoveAVelPoint"
},
{
"Name": "ClickEvent",
"ActionType": "ChangeState",
"ActionParam": "AcceptedState"
},
{
"Name": "CompleteEvent",
"ActionType": "ChangeState",
"ActionParam": "AcceptedState"
}
]
},
{
"Name": "EditEVelPoint",
"EntryMethodName": "EnterEditEVelPoint",
"Event": [
{
"Name": "MotionEvent",
"ActionType": "InvokeMethod",
"ActionParam": "MoveEVelPoint"
},
{
"Name": "ClickEvent",
"ActionType": "ChangeState",
"ActionParam": "CompleteEVelPointState"
},
{
"Name": "CompleteEvent",
"ActionType": "InvokeMethod",
"ActionParam": "AcceptedState"
}
]
},
{
"Name": "EditSVelPoint",
"EntryMethodName": "EnterEditSVelPoint",
"Event": [
{
"Name": "MotionEvent",
"ActionType": "InvokeMethod",
"ActionParam": "MoveSVelPoint"
},
{
"Name": "ClickEvent",
"ActionType": "ChangeState",
"ActionParam": "CompleteSVelPointState"
},
{
"Name": "CompleteEvent",
"ActionType": "InvokeMethod",
"ActionParam": "AcceptedState"
}
]
},
{
"Name": "EditAVelPoint",
"EntryMethodName": "EnterEditAVelPoint",
"Event": [
{
"Name": "MotionEvent",
"ActionType": "InvokeMethod",
"ActionParam": "MoveAVelPoint"
},
{
"Name": "ClickEvent",
"ActionType": "ChangeState",
"ActionParam": "CompleteAVelPointState"
},
{
"Name": "CompleteEvent",
"ActionType": "InvokeMethod",
"ActionParam": "AcceptedState"
}
]
},
{
"Name": "EditManualEVelPoint",
"EntryMethodName": "EnterEditManualEVelPoint",
"Event": [
{
"Name": "MotionEvent",
"ActionType": "InvokeMethod",
"ActionParam": "MoveEVelPoint"
},
{
"Name": "ClickEvent",
"ActionType": "ChangeState",
"ActionParam": "CompleteEVelPointState"
},
{
"Name": "CompleteEvent",
"ActionType": "InvokeMethod",
"ActionParam": "AcceptedState"
}
]
},
{
"Name": "EditManualSVelPoint",
"EntryMethodName": "EnterEditManualSVelPoint",
"Event": [
{
"Name": "MotionEvent",
"ActionType": "InvokeMethod",
"ActionParam": "MoveSVelPoint"
},
{
"Name": "ClickEvent",
"ActionType": "ChangeState",
"ActionParam": "CompleteSVelPointState"
},
{
"Name": "CompleteEvent",
"ActionType": "InvokeMethod",
"ActionParam": "AcceptedState"
}
]
},
{
"Name": "EditManualAVelPoint",
"EntryMethodName": "EnterEditManualAVelPoint",
"Event": [
{
"Name": "MotionEvent",
"ActionType": "InvokeMethod",
"ActionParam": "MoveAVelPoint"
},
{
"Name": "ClickEvent",
"ActionType": "ChangeState",
"ActionParam": "CompleteAVelPointState"
},
{
"Name": "CompleteEvent",
"ActionType": "InvokeMethod",
"ActionParam": "AcceptedState"
}
]
},
{
"Name": "CompleteEVelPointState",
"EntryMethodName": "EnterCompleteEVelPoint",
"Event": [
{
"Name": "MotionEvent",
"ActionType": "InvokeMethod",
"ActionParam": "CheckNextMeas"
},
{
"Name": "ClickEvent",
"ActionType": "ChangeState",
"ActionParam": "EditEVelPoint"
},
{
"Name": "RotaryEncoderInc",
"ActionType": "InvokeMethod",
"ActionParam": "EncoderInc"
},
{
"Name": "RotaryEncoderDec",
"ActionType": "InvokeMethod",
"ActionParam": "EncoderDec"
},
{
"Name": "CompleteEvent",
"ActionType": "ChangeState",
"ActionParam": "AcceptedState"
}
]
},
{
"Name": "CompleteSVelPointState",
"EntryMethodName": "EnterCompleteSVelPoint",
"Event": [
{
"Name": "MotionEvent",
"ActionType": "InvokeMethod",
"ActionParam": "CheckNextMeas"
},
{
"Name": "ClickEvent",
"ActionType": "ChangeState",
"ActionParam": "EditSVelPoint"
},
{
"Name": "RotaryEncoderInc",
"ActionType": "InvokeMethod",
"ActionParam": "EncoderInc"
},
{
"Name": "RotaryEncoderDec",
"ActionType": "InvokeMethod",
"ActionParam": "EncoderDec"
},
{
"Name": "CompleteEvent",
"ActionType": "ChangeState",
"ActionParam": "AcceptedState"
}
]
},
{
"Name": "CompleteAVelPointState",
"EntryMethodName": "EnterCompleteAVelPoint",
"Event": [
{
"Name": "MotionEvent",
"ActionType": "InvokeMethod",
"ActionParam": "CheckNextMeas"
},
{
"Name": "ClickEvent",
"ActionType": "ChangeState",
"ActionParam": "EditAVelPoint"
},
{
"Name": "RotaryEncoderInc",
"ActionType": "InvokeMethod",
"ActionParam": "EncoderInc"
},
{
"Name": "RotaryEncoderDec",
"ActionType": "InvokeMethod",
"ActionParam": "EncoderDec"
},
{
"Name": "CompleteEvent",
"ActionType": "ChangeState",
"ActionParam": "AcceptedState"
}
]
},
{
"Name": "AcceptedState",
"EntryMethodName": "EnterCompleteToolPoints",
"Event": [
{
"Name": "ClickStartPoint",
"ActionType": "InvokeMethod",
"ActionParam": "EnterSVelPointEdit"
},
{
"Name": "ClickEndPoint",
"ActionType": "InvokeMethod",
"ActionParam": "EnterEVelPointEdit"
},
{
"Name": "ClickAVelPoint",
"ActionType": "InvokeMethod",
"ActionParam": "EnterAVelPointEdit"
},
{
"Name": "ClickEvent",
"ActionType": "InvokeMethod",
"ActionParam": "SelectManualToolStart"
}
]
}
]
}
} FSMBase.h
#pragma once
#include #include #include
class FSMBase { public: std::string StateName; std::string EventName; std::vector<FSMBase*> subitem; FSMBase* parent;
FSMBase(); void getdata(); virtual void Add(FSMBase* b1); ~FSMBase(); };
FSMBase.cpp
#include #include #include #include<rapidjson/document.h> #include<rapidjson/filereadstream.h> #include #include "FSMBase.h" #include "PrimitiveTool.h" #include "Event.h" #include "State.h" #include "FSMBuilder.h"
using namespace std; using namespace rapidjson;
FSMBase::FSMBase() { std::cout << "default constructor called" << std::endl; }
void FSMBase::getdata() { std::cout << "FSMBase called" << std::endl; }
void FSMBase::Add(FSMBase* b1) { subitem.push_back(b1); b1->getdata(); }
FSMBase::~FSMBase() { std::cout << " destructor called" << std::endl; }
int main() {
Document doc; ifstream file("FSMImplementationJSON.json"); if (!file.is_open()) { cerr << "Could not open file: FSMImplementationJSON.json" << endl; return 1; }
string json((istreambuf_iterator(file)), istreambuf_iterator());
doc.Parse(json.c_str());
if (doc.HasParseError()) { cerr << "Error parsing JSON: " << doc.GetParseError() << endl; return 1; } PrimitiveTool* prTool1 = new PrimitiveTool(); 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("CompleteEvent", "ChangeState", "WaitForAccept"); prTool1->InEvent(event1);
std::cout << "Active State after CompleteEvent: " << (prTool1->GetActiveStatus() ? prTool1->GetActiveStatus()->StateName : "None") << std::endl; Event* event2 = new Event("CompleteEvent", "ChangeState", "AcceptedState"); prTool1->InEvent(event2);
std::cout << "Active State after CompleteEvent 2: " << (prTool1->GetActiveStatus() ? prTool1->GetActiveStatus()->StateName : "None") << std::endl;
delete event1; delete prTool1; return 0; }
FSMBuilder.cpp
#include "FSMBuilder.h" #include "rapidjson/document.h" #include
using namespace rapidjson; using namespace std;
void FSMBuilder::createStatesFromJson(PrimitiveTool* primitiveTool, rapidjson::Document& doc) { if (doc.HasMember("PrimitiveTool") && doc["PrimitiveTool"].HasMember("State") && doc["PrimitiveTool"]["State"].IsArray()) { const Value& states = doc["PrimitiveTool"]["State"]; for (SizeType i = 0; i < states.Size(); ++i) { const Value& stateVal = states[i]; State* StateObj = new State(primitiveTool);
if (stateVal.HasMember("Name") && stateVal["Name"].IsString()) { StateObj->StateName = stateVal["Name"].GetString(); StateObj->EntreyMethodName = stateVal.HasMember("EntryMethodName") ? stateVal["EntryMethodName"].GetString() : ""; std::cout << "State: " << StateObj->StateName << "\n Entry method : " << StateObj->EntreyMethodName << std::endl; }
if (stateVal.HasMember("Event")) { if (stateVal["Event"].IsObject()) { const rapidjson::Value& eventVal = stateVal["Event"]; Event* eventObj = new Event(StateObj); if (eventVal.HasMember("Name") && eventVal["Name"].IsString()) { eventObj->EventName = eventVal["Name"].GetString(); eventObj->ActionType = eventVal.HasMember("ActionType") ? eventVal["ActionType"].GetString() : ""; eventObj->ActionParam = eventVal.HasMember("ActionParam") ? eventVal["ActionParam"].GetString() : ""; std::cout << " EventName : " << eventObj->EventName << "\n ActionType : " << eventObj->ActionType << "\n ActionParam :" << eventObj->ActionParam << std::endl; StateObj->Add(eventObj); } } else if (stateVal["Event"].IsArray()) { const Value& events = stateVal["Event"]; for (SizeType j = 0; j < events.Size(); ++j) { const Value& eventVal = events[j]; Event* EventObj = new Event(StateObj);
if (eventVal.HasMember("Name") && eventVal["Name"].IsString()) { string eventName = eventVal["Name"].GetString(); string actionType = eventVal.HasMember("ActionType") ? eventVal["ActionType"].GetString() : ""; string actionParam = eventVal.HasMember("ActionParam") ? eventVal["ActionParam"].GetString() : ""; EventObj->EventName = eventName; EventObj->ActionType = actionType; EventObj->ActionParam = actionParam; std::cout << "Created Event: " << EventObj->EventName<< ", Action Type: " << EventObj->ActionType<< ", Action Param: " << EventObj->ActionParam << std::endl; StateObj->Add(EventObj); } } } } primitiveTool->Add(StateObj); } } }
PrimitiveTool.cpp
void PrimitiveTool::Add(FSMBase* b1) {
subitem.push_back(b1); if (State* state = dynamic_cast<State*>(b1)) { state->parent = this; currentState = state; } }
void PrimitiveTool::SetInitialState(const std::string& stateName) { State* initialState = Search(stateName); if (initialState) { currentState = initialState; //std::cout << "Initial state set to: " << currentState->StateName << std::endl; } else { std::cout << "State not found: " << stateName << std::endl; } }
void PrimitiveTool::InEvent(Event* event) { if (!currentState) return; State* state = Search(currentState->StateName); if (state) { //std::cout << " searched state is : " <<StateName<< std::endl; state->InEvent(event); } }
State* PrimitiveTool::GetActiveStatus() { return currentState; }
State* PrimitiveTool::Search(const std::string& name) { for (FSMBase* item : subitem) { State* state = dynamic_cast<State*>(item); if (state && state->StateName == name) { std::cout << "Found STATE :" << name << std::endl; return state; } } return nullptr; }
void PrimitiveTool::ChangeState(const std::string& newStateName) { State* newState = Search(newStateName); if (newState) { currentState = newState; std::cout << "Changed to state: " << currentState->StateName << std::endl; } else { std::cout << "not found state: " << newStateName << std::endl; } }
State.h
#pragma once #include "FSMBase.h" #include "Event.h" #include "PrimitiveTool.h" class Event; class PrimitiveTool; class FSMBase;
class State : public FSMBase { private: PrimitiveTool* parentTool;
public: State(PrimitiveTool* toolParent) : parentTool(toolParent) {
}
std::string EntreyMethodName;
std::vector<Event*> eventitem;
void Add(FSMBase* fsmbase1);
void getdata(const std::string Name, const std::string Methodname);
void InEvent(Event* event);
Event* Search(const std::string& name);
};
State.cpp
#include "State.h"
void State::InEvent(Event* event) { Event* matchedevent = Search(event->EventName); if (matchedevent) { matchedevent -> InEvent(event); std::cout << " matched event is : " << matchedevent->EventName << "action type is : " << matchedevent->ActionType<<std::endl; } else { std::cout << "NO MATCHING EVENT FOUND" << std::endl; } }
Event* State::Search(const std::string& name) { for (Event* event : eventitem) { if (event->EventName == name) { std::cout << "Found event :" << name << std::endl; return event; } } return nullptr; }
void State::ChangeState(Event* event) { if (event->ActionType == "ChangeState") { if (parentTool) { parentTool->ChangeState(event->ActionParam); } } }
void State::Add(FSMBase* fsmbase1) { subitem.push_back(fsmbase1); if (Event* event = dynamic_cast<Event*>(fsmbase1)) { eventitem.push_back(event); std::cout << "ADding event:" << event->EventName << " to state : " << StateName << std::endl; } }
Event.h
#pragma once #include "FSMBase.h" #include "PrimitiveTool.h"
class State; class PrimitiveTool;
class Event : public FSMBase { private: State* parentState; public: Event(const std::string& eventName, const std::string& eventType, const std::string& eventTarget); Event(State* stateParent) : parentState(stateParent) {}
std::string EventName;
std::string ActionType;
std::string ActionParam;
std::string getActionType() const;
std::string getName() const;
void InEvent(Event* event);
};
Event.cpp #include "Event.h"
Event::Event(const std::string& eventName, const std::string& eventType, const std::string& eventTarget) { EventName = eventName; ActionType = eventType; ActionParam = eventTarget; }
std::string Event::getActionType() const { return ActionType; }
std::string Event::getName() const { return EventName; }
void Event::InEvent(Event* event) { if (ActionType=="ChangeState") { if (parentState) { parentState->ChangeState(event); }
} else if(ActionType=="InvokeMethod") { std::cout << "Invoke method called" << std::endl; } }
void FSMBase::EnterSVelPointEdit() { std::cout << "EnterSVelPointEdit method invoked" << std::endl; } void Event::InEvent(Event* event) { if (ActionType=="ChangeState") { if (parentState) { parentState->ChangeState(event); }
} else if(ActionType=="InvokeMethod") { std::cout << "Invoke method called" << std::endl; } }
if(ActionType=="InvokeMethod") calls i want to invoke a method in the "ActionParam=EnterSVelPointEdit" whiere EnterSVelPointEdit() is defined in PrimitiveTool class using function pointer
To invoke a method of the PrimitiveTool class using a function pointer based on the ActionParam in the Event class, you can store a mapping of string parameter values (like "EnterSVelPointEdit") to pointers to member functions of PrimitiveTool. Below, I'll guide you through the necessary modifications to implement this functionality:
You can define a function pointer type for methods in the PrimitiveTool class. For example, if you have multiple methods to invoke, a typedef will help:
typedef void (PrimitiveTool::*MethodPointer)();
Create a mapping between your ActionParam strings and their corresponding function pointers to methods in the PrimitiveTool class. You can do this in your PrimitiveTool class or globally.
Add a member variable to PrimitiveTool to store this mapping:
#include <unordered_map>
// ...
class PrimitiveTool {
// ...
std::unordered_map<std::string, MethodPointer> methodMap;
void InitializeMethodMap() {
methodMap["EnterSVelPointEdit"] = &PrimitiveTool::EnterSVelPointEdit;
// Add more methods as needed
}
public:
// ...
void EnterSVelPointEdit() {
std::cout << "EnterSVelPointEdit method invoked" << std::endl;
}
void InvokeMethodByParam(const std::string& actionParam) {
auto it = methodMap.find(actionParam);
if (it != methodMap.end()) {
(this->*(it->second))(); // Invoke the method pointed to
} else {
std::cout << "No method found for action parameter: " << actionParam << std::endl;
}
}
};
In the Event class, modify the InEvent method to call the appropriate function based on the ActionParam when ActionType is "InvokeMethod".
void Event::InEvent(Event* event) {
if (ActionType == "ChangeState") {
if (parentState) {
parentState->ChangeState(event);
}
}
else if (ActionType == "InvokeMethod") {
if (parentState) {
PrimitiveTool* toolParent = dynamic_cast<PrimitiveTool*>(parentState->parentTool);
if (toolParent) {
std::cout << "Invoking method: " << ActionParam << std::endl;
toolParent->InvokeMethodByParam(ActionParam);
}
}
}
}
Don't forget to call InitializeMethodMap after you create your PrimitiveTool object:
PrimitiveTool* prTool1 = new PrimitiveTool();
prTool1->InitializeMethodMap(); // Initialize the method mapping
FSMBuilder::createStatesFromJson(prTool1, doc);
Here's how the focused area of your code will look after these changes:
PrimitiveTool.h:#include <unordered_map>
// Inside the PrimitiveTool class definition
void InitializeMethodMap();
void InvokeMethodByParam(const std::string& actionParam);
PrimitiveTool.cpp:void PrimitiveTool::InitializeMethodMap() {
methodMap["EnterSVelPointEdit"] = &PrimitiveTool::EnterSVelPointEdit;
// Register other methods similarly...
}
void PrimitiveTool::InvokeMethodByParam(const std::string& actionParam) {
auto it = methodMap.find(actionParam);
if (it != methodMap.end()) {
(this->*(it->second))(); // Invoke the method pointed to
} else {
std::cout << "No method found for action parameter: " << actionParam << std::endl;
}
}
By implementing these changes, you should be able to invoke the EnterSVelPointEdit function (and similar functions) dynamically based on the ActionParam specified in your JSON events.
i am getting below error Severity Code Description Project File Line Suppression State Details Error C2504 'FSMBase': base class undefined FsmImplementation C:\Users\1038586\Downloads\FsmImplementation11142024\FsmImplementation\FsmImplementation\PrimitiveTool.h 11 Error C2504 'FSMBase': base class undefined FsmImplementation C:\Users\1038586\Downloads\FsmImplementation11142024\FsmImplementation\FsmImplementation\State.h 11 Error C2504 'FSMBase': base class undefined FsmImplementation C:\Users\1038586\Downloads\FsmImplementation11142024\FsmImplementation\FsmImplementation\PrimitiveTool.h 11 Error C2504 'FSMBase': base class undefined FsmImplementation C:\Users\1038586\Downloads\FsmImplementation11142024\FsmImplementation\FsmImplementation\Event.h 14 Error C2039 'StateName': is not a member of 'State' FsmImplementation C:\Users\1038586\Downloads\FsmImplementation11142024\FsmImplementation\FsmImplementation\FSMBase.cpp 69 Error C2504 'FSMBase': base class undefined FsmImplementation C:\Users\1038586\Downloads\FsmImplementation11142024\FsmImplementation\FsmImplementation\State.h 11 Error C2504 'FSMBase': base class undefined FsmImplementation C:\Users\1038586\Downloads\FsmImplementation11142024\FsmImplementation\FsmImplementation\Event.h 14 Error C2039 'StateName': is not a member of 'State' FsmImplementation C:\Users\1038586\Downloads\FsmImplementation11142024\FsmImplementation\FsmImplementation\FSMBuilder.cpp 18 Error C2039 'StateName': is not a member of 'State' FsmImplementation C:\Users\1038586\Downloads\FsmImplementation11142024\FsmImplementation\FsmImplementation\FSMBuilder.cpp 20 Error C2664 'void State::Add(FSMBase *)': cannot convert argument 1 from 'Event *' to 'FSMBase *' FsmImplementation C:\Users\1038586\Downloads\FsmImplementation11142024\FsmImplementation\FsmImplementation\FSMBuilder.cpp 32 Error C2664 'void State::Add(FSMBase *)': cannot convert argument 1 from 'Event *' to 'FSMBase *' FsmImplementation C:\Users\1038586\Downloads\FsmImplementation11142024\FsmImplementation\FsmImplementation\FSMBuilder.cpp 49 Error C2664 'void PrimitiveTool::Add(FSMBase *)': cannot convert argument 1 from 'State *' to 'FSMBase *' FsmImplementation C:\Users\1038586\Downloads\FsmImplementation11142024\FsmImplementation\FsmImplementation\FSMBuilder.cpp 54 Error C2504 'FSMBase': base class undefined FsmImplementation C:\Users\1038586\Downloads\FsmImplementation11142024\FsmImplementation\FsmImplementation\Event.h 14 Error C2504 'FSMBase': base class undefined FsmImplementation C:\Users\1038586\Downloads\FsmImplementation11142024\FsmImplementation\FsmImplementation\PrimitiveTool.h 11 Error C2061 syntax error: identifier 'FSMBase' FsmImplementation C:\Users\1038586\Downloads\FsmImplementation11142024\FsmImplementation\FsmImplementation\PrimitiveTool.h 18 Error C2504 'FSMBase': base class undefined FsmImplementation C:\Users\1038586\Downloads\FsmImplementation11142024\FsmImplementation\FsmImplementation\Event.h 14
The errors you're encountering generally stem from one of two problems: circular dependencies in header files or a missing class definition. Let's address these issues step-by-step.
Base Class Undefined:
Errors mentioning FSMBase being undefined suggest that this class either hasn't been defined at the time it's being referenced or there’s a circular dependency where the files reference each other in a manner that prevents complete builds.
Member Functions/Variables Not Found:
Errors about StateName not being a member of State, along with the inability to convert types, indicate that there might be issues with class definitions or with how classes are included.
Ensure that you use forward declarations where possible to avoid circular dependencies. For any classes that depend on FSMBase, you probably only need to declare them, not define them.
For example, in Event.h, State.h, and PrimitiveTool.h, add forward declarations for FSMBase:
// At the top of the header files, before including other headers
class FSMBase; // Forward declaration of FSMBase
This tells the compiler that FSMBase is a class, allowing it to compile without needing the full definition first.
Ensure that each header file has include guards to prevent it from being included multiple times in a single compilation unit.
Example for each header file:
#ifndef FSMBASE_H
#define FSMBASE_H
// ... existing code ...
#endif // FSMBASE_H
Do this for each of your header files.
Make sure that your includes are properly structured. For example, Event.h should include PrimitiveTool.h and State.h, but PrimitiveTool.h does not need to include Event.h or State.h if it only references them through pointers. Here's how you can organize your includes:
FSMBase.h:
#pragma once
#include <iostream>
#include <string>
#include <vector>
class FSMBase {
public:
std::string StateName;
std::string EventName;
std::vector<FSMBase*> subitem;
FSMBase* parent;
FSMBase();
void getdata();
virtual void Add(FSMBase* b1);
~FSMBase();
};
State.h:
#pragma once
#include "FSMBase.h"
#include <vector>
#include <string>
class PrimitiveTool; // Forward declaration of PrimitiveTool
class State : public FSMBase {
private:
PrimitiveTool* parentTool;
public:
State(PrimitiveTool* toolParent);
std::string EntreyMethodName;
std::vector<Event*> eventitem;
void Add(FSMBase* fsmbase1);
void InEvent(Event* event);
Event* Search(const std::string& name);
};
Event.h:
#pragma once
#include "FSMBase.h"
class State; // Forward declaration
class PrimitiveTool; // Forward declaration
class Event : public FSMBase {
private:
State* parentState;
public:
Event(const std::string& eventName, const std::string& eventType, const std::string& eventTarget);
Event(State* stateParent);
std::string EventName;
std::string ActionType;
std::string ActionParam;
std::string getActionType() const;
std::string getName() const;
void InEvent(Event* event);
};
PrimitiveTool.h:
#pragma once
#include "FSMBase.h"
#include <unordered_map>
class State; // Forward declaration
class Event; // Forward declaration
class PrimitiveTool : public FSMBase {
private:
std::unordered_map<std::string, MethodPointer> methodMap;
public:
void InitializeMethodMap();
void InvokeMethodByParam(const std::string& actionParam);
void EnterSVelPointEdit();
};
.cpp FilesDouble-check that all member functions that are declared in the headers have corresponding implementations in the .cpp files. Sometimes forgetting to define these can also lead to compilation issues.
Following these steps should help you resolve the errors you are facing. Most importantly, the organization of includes and class dependencies is crucial in C++. If issues persist even after these changes, consider if specific class definitions are being called too soon or if you need further restructuring of your code logic.
#include "Event.h"
typedef void(*FunctionPointer)();
std::map<std::string, FunctionPointer>
functionmap = { {"EnterSVelPointEdit",&EnterSVelPointEdit},{"EnterEVelPointEdit", &EnterEVelPointEdit} };
Event::Event(const std::string& eventName, const std::string& eventType, const std::string& eventTarget) { EventName = eventName; ActionType = eventType; ActionParam = eventTarget; }
std::string Event::getActionType() const { return ActionType; }
std::string Event::getName() const { return EventName; }
void Event::InEvent(Event* event) { if (ActionType=="ChangeState") { if (parentState) { parentState->ChangeState(event); }
}
else if(ActionType=="InvokeMethod")
{
std::cout << "Invoke method called" << std::endl;
auto it = functionmap.find(event->ActionParam);
if (it != functionmap.end()) {
FunctionPointer func = it->second;
func();
}
else {
std::cout << "no action method set for InvokeMethod" << std::endl;
}
}
}
i am getting below error
Severity Code Description Project File Line Suppression State Details Error LNK2005 "void cdecl EnterEVelPointEdit(void)" (?EnterEVelPointEdit@@YAXXZ) already defined in Event.obj FsmImplementation C:\Users\1038586\Downloads\FsmImplementation11142024\FsmImplementation\FsmImplementation\PrimitiveTool.obj 1 Warning C26495 Variable 'Event::parentState' is uninitialized. Always initialize a member variable (type.6). FsmImplementation C:\Users\1038586\Downloads\FsmImplementation11142024\FsmImplementation\FsmImplementation\Event.cpp 11 Warning C26495 Variable 'rapidjson::internal::Double::::d' is uninitialized. Always initialize a member variable (type.6). FsmImplementation C:\Users\1038586\Downloads\FsmImplementation11142024\FsmImplementation\rapidjson-master\include\rapidjson\internal\ieee754.h 25 Warning C26495 Variable 'rapidjson::internal::Double::::u' is uninitialized. Always initialize a member variable (type.6). FsmImplementation C:\Users\1038586\Downloads\FsmImplementation11142024\FsmImplementation\rapidjson-master\include\rapidjson\internal\ieee754.h 25 Error LNK2005 "void __cdecl EnterEVelPointEdit(void)" (?EnterEVelPointEdit@@YAXXZ) already defined in Event.obj FsmImplementation C:\Users\1038586\Downloads\FsmImplementation11142024\FsmImplementation\FsmImplementation\FSMBase.obj 1 Error LNK2005 "void __cdecl EnterSVelPointEdit(void)" (?EnterSVelPointEdit@@YAXXZ) already defined in Event.obj FsmImplementation C:\Users\1038586\Downloads\FsmImplementation11142024\FsmImplementation\FsmImplementation\FSMBase.obj 1 Error LNK2005 "void __cdecl EnterEVelPointEdit(void)" (?EnterEVelPointEdit@@YAXXZ) already defined in Event.obj FsmImplementation C:\Users\1038586\Downloads\FsmImplementation11142024\FsmImplementation\FsmImplementation\FSMBuilder.obj 1 Error LNK2005 "void __cdecl EnterSVelPointEdit(void)" (?EnterSVelPointEdit@@YAXXZ) already defined in Event.obj FsmImplementation C:\Users\1038586\Downloads\FsmImplementation11142024\FsmImplementation\FsmImplementation\FSMBuilder.obj 1 Error LNK2005 "void __cdecl EnterSVelPointEdit(void)" (?EnterSVelPointEdit@@YAXXZ) already defined in Event.obj FsmImplementation C:\Users\1038586\Downloads\FsmImplementation11142024\FsmImplementation\FsmImplementation\PrimitiveTool.obj 1 Error LNK2005 "void __cdecl EnterEVelPointEdit(void)" (?EnterEVelPointEdit@@YAXXZ) already defined in Event.obj FsmImplementation C:\Users\1038586\Downloads\FsmImplementation11142024\FsmImplementation\FsmImplementation\State.obj 1 Error LNK2005 "void __cdecl EnterSVelPointEdit(void)" (?EnterSVelPointEdit@@YAXXZ) already defined in Event.obj FsmImplementation C:\Users\1038586\Downloads\FsmImplementation11142024\FsmImplementation\FsmImplementation\State.obj 1 Error LNK1169 one or more multiply defined symbols found FsmImplementation C:\Users\1038586\Downloads\FsmImplementation11142024\FsmImplementation\x64\Debug\FsmImplementation.exe 1
The errors you are encountering are related to multiple definitions of the functions EnterSVelPointEdit and EnterEVelPointEdit. This is leading to linker errors because these functions are defined in more than one translation unit (source file).
To resolve this, we need to adopt some best practices around defining functions in C++. Here’s how to address the issues step-by-step:
You can only define a function once in your entire program. If you need to use these methods as function pointers but use them in multiple files, you should declare them in one source file and define them in a single translation unit.
Solution:
EnterSVelPointEdit and EnterEVelPointEdit are defined in one source file (e.g., PrimitiveTool.cpp) and declared in their respective header files (PrimitiveTool.h).PrimitiveTool.h)#pragma once
class PrimitiveTool {
public:
void EnterSVelPointEdit();
void EnterEVelPointEdit();
};
// Add other members or forward declarations
PrimitiveTool.cpp)#include "PrimitiveTool.h"
#include <iostream>
void PrimitiveTool::EnterSVelPointEdit() {
std::cout << "EnterSVelPointEdit method invoked" << std::endl;
}
void PrimitiveTool::EnterEVelPointEdit() {
std::cout << "EnterEVelPointEdit method invoked" << std::endl;
}
In your Event.cpp file where you set up the functionmap, be sure to use function pointers linked to the instance of PrimitiveTool. You cannot use the class methods directly without an instance unless they are static.
Here’s how the Event class might utilize these:
Event.cpp)Make sure that your functionmap is correctly utilizing instance methods:
#include <map>
#include <string>
#include <iostream>
#include "Event.h"
#include "PrimitiveTool.h" // Include the PrimitiveTool header
typedef void(PrimitiveTool::*FunctionPointer)(); // Pointer to member function of PrimitiveTool
std::map<std::string, FunctionPointer> functionmap;
void InitializeFunctionMap() {
functionmap["EnterSVelPointEdit"] = &PrimitiveTool::EnterSVelPointEdit;
functionmap["EnterEVelPointEdit"] = &PrimitiveTool::EnterEVelPointEdit;
}
void Event::InEvent(Event* event, PrimitiveTool* toolParent) // Add PrimitiveTool as parameter
{
if (ActionType == "ChangeState") {
if (parentState) {
parentState->ChangeState(event);
}
}
else if (ActionType == "InvokeMethod") {
std::cout << "Invoke method called" << std::endl;
auto it = functionmap.find(event->ActionParam);
if (it != functionmap.end()) {
FunctionPointer func = it->second;
(toolParent->*func)(); // Call the method on the PrimitiveTool instance
} else {
std::cout << "No action method set for InvokeMethod" << std::endl;
}
}
}
Ensure that your function map is initialized before any events are processed. This could be done in your PrimitiveTool constructor to avoid scope issues.
EnterSVelPointEdit and EnterEVelPointEdit are defined once and their declarations are placed in the appropriate header file.PrimitiveTool, you need to provide an instance of PrimitiveTool when you invoke them through function pointers in your Event class.By following these steps, the linker errors should be resolved, and your program should compile successfully.
Not yet included in a published release.