![]() |
| | #1 |
| Registered User Join Date: Dec 2002
Posts: 3
| struct nested in a class - member variables initialization I have a class (named tictactwice), and a struct inside (declared as a static variable). The purpose of this struct is to hold two integers, indices into a double array: Code: struct arrayIndex {
int i;
int j;
};
struct arrayIndex TRANSPOSE[4][4]; So far, so good. The problem is trying to initialize this TRANSPOSE array in the implementation file. It just doesn't happen - I don't get a compiler error, but it doesn't work: Code: struct tictactwice::arrayIndex TRANSPOSE[4][4] =
{{{2,3},{0,0},{1,2},{3,1}},
{{1,1},{3,2},{2,0},{0,3}},
{{3,0},{1,3},{0,1},{2,2}},
{{0,2},{2,1},{3,3},{1,0}}};
TRANSPOSE[0][0].i gives 2012756197. But when I try something like: struct tictactwice::boardIndex test = {-1, -1}; // a stand-alone struct IT WORKS - e.g. test.i = -1 and test.j = -1. So, I can initialize each little struct individually but not when they are inside the array. Oh, I also tried placing 16 initialized "test" structs in the array, but it STILL doesn't work: Code: struct tictactwice::arrayIndex TRANSPOSE[4][4] =
{{test,test,test,test},
{test,test,test,test},
{test,test,test,test},
{test,test,test,test}};
Maybe I am accessing the structs in the array wrong - I don't understand what is happening and how come the "test" struct becomes inaccessible once stuck in this array, while it is perfectly initialized when being outside. ANY suggestions and help are very welcome!!! |
| Viana is offline | |
| | #2 |
| Registered User Join Date: Dec 2002
Posts: 3
| WOHOOOO ![]() I found the problem: when I am declaring the array in the implementation file I was doing this: Code: struct tictactwice::boardIndex TRANSPOSE[4][4] =
{{{2,3},{0,0},{1,2},{3,1}},
{{1,1},{3,2},{2,0},{0,3}},
{{3,0},{1,3},{0,1},{2,2}},
{{0,2},{2,1},{3,3},{1,0}}};
Code: struct tictactwice::boardIndex tictactwice::TRANSPOSE[4][4] =
{{{2,3},{0,0},{1,2},{3,1}
{{1,1},{3,2},{2,0},{0,3}},
{{3,0},{1,3},{0,1},{2,2}},
{{0,2},{2,1},{3,3},{1,0}}};
But NOW IT WORKS!!!Thanks all for looking! |
| Viana is offline | |
| | #3 |
| Cheesy Poofs! Join Date: Sep 2002 Location: Boulder
Posts: 1,728
| I'm not sure if these are the problems or if I'm just misunderstanding what it is that you are trying to accomplish, but first: isn't TRANSPOSE a simple array of arrayIndexes? You have it declared as a struct. Second, and I'm pretty sure this is the main problem, TRANSPOSE is an element of tictactwice, therefore also needs the class name in front of it, otherwise you are declaring a NEW array called TRANSPOSE outside of the class - thus the one inside the class is still not initializes therefore giving you garbage. Try this and see if it works: Code: struct tictactwice::arrayIndex TRANSPOSE[4][4] = blah // you've written tictactwice::arrayIndex tictactwice::TRANSPOSE[4][4] = blah // try this |
| PJYelton is offline | |
| | #4 |
| Cheesy Poofs! Join Date: Sep 2002 Location: Boulder
Posts: 1,728
| LOL, we figured it out at the same time! Congrats! |
| PJYelton is offline | |
| | #5 |
| Registered User Join Date: Dec 2002
Posts: 3
| heh Thanks PJ Sometimes these ::bla::bla1::bla2 can get overwhelming when you are using 7 of them on the same line...Can't believe this took me hours to figure out, but the most obvious mistakes are the hardest to find (from personal experience....) |
| Viana is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| error: ‘struct tcphdr’ has no member named ‘th_win’ | nasim751 | C Programming | 2 | 04-22-2008 12:07 PM |
| error: ‘struct tcphdr’ has no member named ‘th_seq | nasim751 | C Programming | 2 | 04-19-2008 12:06 AM |
| Struct with a ptr to a dynamically allocated array of other structures :( | michael- | C Programming | 10 | 05-18-2006 11:23 PM |
| Dikumud | maxorator | C++ Programming | 1 | 10-01-2005 06:39 AM |
| gcc problem | bjdea1 | Linux Programming | 13 | 04-29-2002 06:51 PM |