c struct initialization with #include "file.h"

Mar 30, 2014 at 5:17pm
I have a c struct define as: struct p { #include "file.h" }p; there are many warnings being thrown about this struct not being initialized when the code compiles. How can I fix these warnings ? How can I initialize such struct ?
Last edited on Mar 30, 2014 at 5:20pm
Mar 30, 2014 at 5:20pm
Why do you have file.h inside the struct?
Mar 30, 2014 at 5:24pm
This is legacy code. Was written long time ago but I am looking at possible ways to fix these warnings
Last edited on Mar 30, 2014 at 5:25pm
Mar 30, 2014 at 7:59pm
Your code makes more sense now when you have updated it, but it's impossible to say why you get the warnings without seeing the code that cause them or at least the complete warning messages.
Mar 31, 2014 at 7:02am
Exact Error is : File.cpp:26: warning: 'File::m_param' should be initialized in the member initialization list.

File.cpp (line 26 to 31) is :
File::File() :
m_systemInitialized(false)
{
m_numberParams = NUMBER_PARAMS;
m_paramTable = m_localParams;
}

File.h is :
class File: public Base
{
public:
File()
virtual ~File();

protected:

private:
#include "defineParamStruct.h"

struct ParamStruct
{
#include "FileParams.h"
};
ParamTableEntry m_localParams[NUMBER_PARAMS];
ParamStruct m_param;
bool m_systemInitialized;

};

FileParams.h is :

SomeCrash(Some_Device_Crash, someVolt, "none", 0.14286f)
Mar 31, 2014 at 7:17am
I'm a bit confused about this code but the warning is pretty clear. It wants you to initialize m_param in the constructor initialization list, like you do with m_systemInitialized.
Mar 31, 2014 at 7:28am
i understand that it needs to be constructor initialized but i am not sure how this would be done since m_param is an object of a defined by the inclusion of FileParams.h, which is primarily defined by several macros as you can see the example from my earlier post.

Mar 31, 2014 at 7:34am
The thing you need to remember with macros is that the compiler doesn't know that macros exist: They are already removed and replaced with the appropriate counterparts once it reaches the actual compilation stage. Hence, just define like normal, by the time it gets to that point it doesn't matter that it wasn't visible in the source file you wrote it in.
Mar 31, 2014 at 7:40am
What exactly do you mean by "just define like normal" ? are you referring to m_param ?
Last edited on Mar 31, 2014 at 7:41am
Mar 31, 2014 at 7:59am
If you have a created ParamStruct constructor use that otherwise use the implicit defined default constructor.
1
2
3
4
5
6
File::File() :
	m_systemInitialized(false),
	m_param()
{
	...
}


In C++11 you can initialize the struct members directly in the initialization list by listing the values for each member inside {}.
1
2
3
4
5
6
[code]File::File() :
	m_systemInitialized(false),
	m_param{value1, value2, value3}
{
	...
}
Last edited on Mar 31, 2014 at 7:59am
Mar 31, 2014 at 2:29pm
Peter87, I think you may be correct; using the implicit default constructor could fix the warnings. I will give it a shot today and see it that works. will keep you informed.
Topic archived. No new replies allowed.