Thread: array of structs initialization - PLZ help...

  1. #1

    Unhappy array of structs initialization - PLZ help...

    I have a struct as a class member, and an array of these structs as another member. I am trying to initialize the array in my class implementation file and it has been a nightmare so far. The weird thing is, that I can initialize an array of structs without a problem in a normal program, but when I try to do it for the class, I don't get a compiler error, but the struct variables remain uninitialized. Here is what I am doing: tictactoe.h:
    class tictactwice : public game {

    static const int ROWS = 4;
    static const int COLUMNS = 4;
    // here is the struct
    struct boardIndex {
    int x;
    int y;
    // here is the array
    struct boardIndex TRANSPOSITION[ROWS][COLUMNS];

    tictactwice( );

    .....bla bla

    And I am initializing it in tictactoe.cxx like this:

    struct tictactwice::boardIndex TRANSPOSITION[tictactwice::ROWS][tictactwice::COLUMNS] = {{{2,3},{0,0},{1,2},{3,1}},

    Making this thing compile gave me nightmares, and right after I got happy I discovered that even though it compiles, the structs inside the array do NOT get initialized. I am clueless at this point - I know that this method would work for simple struct and a simple array, but I guess the belonging to a class part messes it up somehow. I REALLY REALLY appreciate your help!

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    make local struct and init it in your constructor then assign it to your member struct.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :-

  3. #3

    Sounds good, but how?

    I am not sure how to do this, a code example would be wonderful!!!

    I tried making what I thought to be a local struct, but then I get a compiler error when trying to assign a "local" array of this struct to the member array.

    Also, did you mean that I have to initialize the struct in the class constructor? I can't remember if structs can have constructors in c++...I am getting confused with what I read about constructors in c#...

    If you can give me an example of how to do this, it would be great!!

    Thanks so much,

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array initialization has me dumbfounded.
    By chad_crider in forum C++ Programming
    Replies: 5
    Last Post: 02-23-2006, 11:13 PM
  2. Replies: 12
    Last Post: 12-06-2005, 08:30 PM
  3. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  4. Array of pointers to structs
    By lpmrhahn in forum C Programming
    Replies: 1
    Last Post: 04-17-2004, 09:46 AM
  5. Merge sort please
    By vasanth in forum C Programming
    Replies: 2
    Last Post: 11-09-2003, 12:09 PM