struct nested in a class - member variables initialization

This is a discussion on struct nested in a class - member variables initialization within the C++ Programming forums, part of the General Programming Boards category; I have been working on this for a few hours now and I am unable to resolve the problem. I ...

  1. #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!!!

  2. #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!

  3. #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

  4. #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!

  5. #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....)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 04-22-2008, 12:07 PM
  2. Replies: 2
    Last Post: 04-19-2008, 12:06 AM
  3. Replies: 10
    Last Post: 05-18-2006, 11:23 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM

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