Hi all…
I have a header file that defines some UStruct structures.
I want these structures to be dumb objects, nothing but data containers, so I don’t want them to have any kind of constructors.
In the same header file, I want to create a simple function that sets default values for each variable in my struct… So, I wanted to to do something like this.
void InitMyStruct( MyDataStruct& myStruct )
{
… set values on myStruct…
}
NOTE, this is just a bare function, it’s not attached to a class.
However, when I try to compile, I keep getting linker errors saying that the function is already defined. This is happening on the .OBJ files.
So I am assuming that there needs to be a special way to define a C++ function that is not associated to a class? What would be the proper way to handle this?
But seems like exactly the function that you’re trying to write, a constructor. Or you should be setting the default values through inline initializers instead. Wanting it to be “dumb” has nothing to do with having constructors or destructors.
Other than that you’re probably running into issues because you have a function definition in your header that isn’t inline or static. But really you should only but a declaration for the function in your header and the definition in a cpp file.
Thanks MagForceSeven…
These structs will be sent though JSON parsers and utility’s for dealing with REST calls which is why I wanted to keep all functions out of them. Granted, I have not tried to add a constructor to one and see if the json parser/utilities choke on it or not.
I’ll give it a try, putting the function declaration in the header and the definition in the cpp file.