C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 12-12-2002, 01:59 PM   #1
Registered User
 
Join Date: Dec 2002
Posts: 3
struct nested in a class - member variables initialization

I have been working on this for a few hours now and I am unable to resolve the problem.
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;
};
Then, I need an array of these structs (must hold 16 of them). So it is declared as another static variable as:

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}}};
THIS does not initialize the structs inside - I get junk when I try to access any of them:
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}};
TRANSPOSE[0][0].i = JUNK again....while test.i = -1.
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   Reply With Quote
Old 12-12-2002, 02:20 PM   #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}}};
WHILE the correct way of declaring it is:
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}}};
The difference is that without the tictactwice:: in front of the name of the array, it was uncertain that this variable belongs to the class, so when I was referring to it....who knows what the compiler was thinking I was referring to But NOW IT WORKS!!!
Thanks all for looking!
Viana is offline   Reply With Quote
Old 12-12-2002, 02:26 PM   #3
Cheesy Poofs!
 
PJYelton's Avatar
 
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   Reply With Quote
Old 12-12-2002, 02:27 PM   #4
Cheesy Poofs!
 
PJYelton's Avatar
 
Join Date: Sep 2002
Location: Boulder
Posts: 1,728
LOL, we figured it out at the same time! Congrats!
PJYelton is offline   Reply With Quote
Old 12-12-2002, 02:32 PM   #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   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 03:24 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22