I want to create a C + + singleton to support operations,But when I create a singleton class in C + +, I can’t compile it.
code and compilation errors ↓
GeneralAssistCalculateLibrary. H (39): [c4273] “instance”: inconsistent DLL links
GeneralAssistCalculateLibrary. H (39): [c2491] “generalassistcalculatelibrary:: instance”: the definition of dllimport static data member is not allowed
If it’s just static utility functions, why instantiate it at all? Just make the functions static. TSharedPtr isn’t going to make any difference here.
Also, you shouldn’t implement static objects in the header. You can’t define them more than once and if you include that header in more than one file you’ll get conflicting defintions. They need to be in a cpp file.
Or if you must define a singleton in the header for some reason (such as for a class that has no implementation file), do so within the function that returns the singleton. For instance:
That said, singletons are not always the right design pattern to use, and I kind of suspect that’s the case here.
If it’s just a collection of utility functions with no actual object state that requires instantiation, then as TheJamsh says, it’d probably make more sense to implement as a Blueprint Function Library. Not only will that serve the purpose you seem to be aiming for here (and avoid the need to allocate – and eventually, free – an instance of the class if you have a class that you actually have to allocate as a pointer), but you’ll also be able to use those static utility functions as nodes in a Blueprint if needed!