Cannot initialize array of classes except as a local variable.
-
I hope I can explain this. I need to have an array of classes ( CTrackSwitch) as a variable in a class (COpenTrack). class COpenTrack { public: CTrackSwitch trackSwitch[10]; COpenTrack(void); …. I can initialize each array class using constructors, each with different parameters. But initialization only works when done in a method of the “mother” class. I did try to do it in the COpenTrack constructor, it compiles but there are no data in an array classes. I am missing something. boolean COpenTrack ::SwitchRoute( int iTrack) { // TODO works as local variable only - not as COpenTrack variable CTrackSwitch trackSwitch[] = { CTrackSwitch(0,2,8,4),// normal reverse, input , switch start with even tracks from 0 CTrackSwitch(4,6,10,6),// normal reverse, input , switch CTrackSwitch(1,3,9,3),// normal reverse, input , switch CTrackSwitch(5,7,11,5),// normal reverse, input , switch CTrackSwitch(8,10,12,2),// normal reverse, input , switch CTrackSwitch(9,11,13,1),// normal reverse, input , switch CTrackSwitch(12,13,14,0)// normal reverse, input , switch }; ….. Any help would be appreciated. Cheers Vaclav
-
I hope I can explain this. I need to have an array of classes ( CTrackSwitch) as a variable in a class (COpenTrack). class COpenTrack { public: CTrackSwitch trackSwitch[10]; COpenTrack(void); …. I can initialize each array class using constructors, each with different parameters. But initialization only works when done in a method of the “mother” class. I did try to do it in the COpenTrack constructor, it compiles but there are no data in an array classes. I am missing something. boolean COpenTrack ::SwitchRoute( int iTrack) { // TODO works as local variable only - not as COpenTrack variable CTrackSwitch trackSwitch[] = { CTrackSwitch(0,2,8,4),// normal reverse, input , switch start with even tracks from 0 CTrackSwitch(4,6,10,6),// normal reverse, input , switch CTrackSwitch(1,3,9,3),// normal reverse, input , switch CTrackSwitch(5,7,11,5),// normal reverse, input , switch CTrackSwitch(8,10,12,2),// normal reverse, input , switch CTrackSwitch(9,11,13,1),// normal reverse, input , switch CTrackSwitch(12,13,14,0)// normal reverse, input , switch }; ….. Any help would be appreciated. Cheers Vaclav
if you can make trackSwitch static, you can do this:
class COpenTrack
{
public:static CTrackSwitch _trackSwitch[10];
COpenTrack(void)
{
}
};// outside the class definition
CTrackSwitch COpenTrack::_trackSwitch[] =
{
CTrackSwitch(0,2,8,4),// normal reverse, input , switch start with even tracks from 0
CTrackSwitch(4,6,10,6),// normal reverse, input , switch
CTrackSwitch(1,3,9,3),// normal reverse, input , switch
CTrackSwitch(5,7,11,5),// normal reverse, input , switch
CTrackSwitch(8,10,12,2),// normal reverse, input , switch
CTrackSwitch(9,11,13,1),// normal reverse, input , switch
CTrackSwitch(12,13,14,0)// normal reverse, input , switch
}; -
if you can make trackSwitch static, you can do this:
class COpenTrack
{
public:static CTrackSwitch _trackSwitch[10];
COpenTrack(void)
{
}
};// outside the class definition
CTrackSwitch COpenTrack::_trackSwitch[] =
{
CTrackSwitch(0,2,8,4),// normal reverse, input , switch start with even tracks from 0
CTrackSwitch(4,6,10,6),// normal reverse, input , switch
CTrackSwitch(1,3,9,3),// normal reverse, input , switch
CTrackSwitch(5,7,11,5),// normal reverse, input , switch
CTrackSwitch(8,10,12,2),// normal reverse, input , switch
CTrackSwitch(9,11,13,1),// normal reverse, input , switch
CTrackSwitch(12,13,14,0)// normal reverse, input , switch
}; -
I hope I can explain this. I need to have an array of classes ( CTrackSwitch) as a variable in a class (COpenTrack). class COpenTrack { public: CTrackSwitch trackSwitch[10]; COpenTrack(void); …. I can initialize each array class using constructors, each with different parameters. But initialization only works when done in a method of the “mother” class. I did try to do it in the COpenTrack constructor, it compiles but there are no data in an array classes. I am missing something. boolean COpenTrack ::SwitchRoute( int iTrack) { // TODO works as local variable only - not as COpenTrack variable CTrackSwitch trackSwitch[] = { CTrackSwitch(0,2,8,4),// normal reverse, input , switch start with even tracks from 0 CTrackSwitch(4,6,10,6),// normal reverse, input , switch CTrackSwitch(1,3,9,3),// normal reverse, input , switch CTrackSwitch(5,7,11,5),// normal reverse, input , switch CTrackSwitch(8,10,12,2),// normal reverse, input , switch CTrackSwitch(9,11,13,1),// normal reverse, input , switch CTrackSwitch(12,13,14,0)// normal reverse, input , switch }; ….. Any help would be appreciated. Cheers Vaclav
What about something like:
COpenTrack::COpenTrack()
{
trackSwitch[0] = CTrackSwitch(0,2,8,4),// normal reverse, input , switch start with even tracks from 0
trackSwitch[1] = CTrackSwitch(4,6,10,6),// normal reverse, input , switch
...
}"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
-
What about something like:
COpenTrack::COpenTrack()
{
trackSwitch[0] = CTrackSwitch(0,2,8,4),// normal reverse, input , switch start with even tracks from 0
trackSwitch[1] = CTrackSwitch(4,6,10,6),// normal reverse, input , switch
...
}"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
David, it works, but it is "not cool", just "plain" variable initialization. I did try to initialize the whole array in default constructor and it did not fly. I would probably get more descriptive error messages from some other IDE to figure it out. It should be same syntax I used for the CTrackSwitch, but I thing the array is messing it up. .... int iNormal; // passing thru int iReverse; // turning int iInputTrack; // int iSwitch; // may be usefull elesewhere // initialize using constructor CTrackSwitch(int iNormalInit,int iReverseInit,int iInputTrackInit, int iSwitchInit ) { iNormal = iNormalInit; iReverse = iReverseInit; iInputTrack = iInputTrackInit; iSwitch = iSwitchInit; } .... This is one attempt which actually compiled COpenTrack(class CTrackSwitch() ) { trackSwitch[10] = CTrackSwitch(); } but I could not get the constructor to work. Oh well, thanks for your help. Cheers Vaclav
-
I hope I can explain this. I need to have an array of classes ( CTrackSwitch) as a variable in a class (COpenTrack). class COpenTrack { public: CTrackSwitch trackSwitch[10]; COpenTrack(void); …. I can initialize each array class using constructors, each with different parameters. But initialization only works when done in a method of the “mother” class. I did try to do it in the COpenTrack constructor, it compiles but there are no data in an array classes. I am missing something. boolean COpenTrack ::SwitchRoute( int iTrack) { // TODO works as local variable only - not as COpenTrack variable CTrackSwitch trackSwitch[] = { CTrackSwitch(0,2,8,4),// normal reverse, input , switch start with even tracks from 0 CTrackSwitch(4,6,10,6),// normal reverse, input , switch CTrackSwitch(1,3,9,3),// normal reverse, input , switch CTrackSwitch(5,7,11,5),// normal reverse, input , switch CTrackSwitch(8,10,12,2),// normal reverse, input , switch CTrackSwitch(9,11,13,1),// normal reverse, input , switch CTrackSwitch(12,13,14,0)// normal reverse, input , switch }; ….. Any help would be appreciated. Cheers Vaclav
In C++11 you should be able to use an initializer list just as you did in your local variable example. See http://stackoverflow.com/questions/161790/initialize-a-const-array-in-a-class-initializer-in-c[^] That said, of course your compiler needs to support it before you can use that syntax.
GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)