Results 1 to 23 of 23

Thread: array of struct by reference ** RESOLVED!!!**

  1. #1

    Thread Starter
    Frenzied Member ae_jester's Avatar
    Join Date
    Jun 2001
    Location
    Kitchener Ontario Canada Earth
    Posts
    1,545

    array of struct by reference ** RESOLVED!!!**

    Hi,

    I need help setting up the prototype, the actual function header, and the code used to modify the structure being sent to the function. I need to pass the array of this structure by reference so that any changes made change the original data sent in.

    THis is what I have so far.

    VB Code:
    1. 'My prototype
    2. int getlegalmoves(struct postype pos, struct movetype *movelist[]);
    3.  
    4. 'The function code
    5. int getlegalmoves(struct postype pos, struct movetype *movelist[])
    6. {
    7.     movelist[0].from = 1;
    8.     movelist[0].to = 2;
    9.     movelist[1].from = 3;
    10.     movelist[1].to = 4;
    11. }
    Last edited by ae_jester; Dec 21st, 2003 at 04:36 PM.

  2. #2
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    I am sooo not good at this....but if you to pass it as a reference...why are you using * and not &???


    And BTW isn't movelist[] just a pointer to that array. So you are actually all ready changing the originale.....ro am I wrong?

  3. #3

    Thread Starter
    Frenzied Member ae_jester's Avatar
    Join Date
    Jun 2001
    Location
    Kitchener Ontario Canada Earth
    Posts
    1,545
    I am sooo not good at this....
    I am worse, which is why I am asking. I'm trying to convert a vb program to c++ and its becoming a nightmare!

  4. #4
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    I am pretty sure it is a pointer....so you don't need the ? in front of it...that will just make it a pointer to a pointer...and if you have a pointer you are maybe copying the pointer....but that is no work at all. But you are still working on the same struckt as before using that pointer....Thats at least what I would have written on my exam........hehe

  5. #5

    Thread Starter
    Frenzied Member ae_jester's Avatar
    Join Date
    Jun 2001
    Location
    Kitchener Ontario Canada Earth
    Posts
    1,545
    Ok, I changed it to the following, but I still get an error (same one) on the line where I'm calling this function ("local function definitions are illegal")...here is all the relevant code...

    VB Code:
    1. struct postype
    2. {
    3.     int blacks;
    4.     int whites;
    5.     int kings;
    6.     int colour;
    7.     int hashval;
    8. }game;
    9.  
    10. struct movetype
    11. {
    12.     int blacks;
    13.     int whites;
    14.     int kings;
    15.     short from;
    16.     short to;
    17. };
    18.  
    19. 'prototypes
    20. int getlegalmoves(struct postype pos, struct movetype movelist[20]);
    21. int alphabeta(struct postype pos, int depth, int alpha, int beta);
    22.  
    23. int alphabeta(struct postype pos, int depth, int alpha, int beta)
    24. {
    25.     struct movetype movelist[20];
    26.     int i, j, n, value, localalpha, bestvalue, hashvalue, hashtype, lookupvalue;
    27.     struct postype localpos;
    28.     short sqrfrom, sqrto;
    29.    
    30.     bestvalue = -1000;
    31.     localpos = pos;
    32.     localalpha = alpha;
    33.    
    34.     if (depth==0)
    35.     {
    36.         value=evaluation(pos);
    37.         return value;
    38.     }
    39.    
    40.     //n = getlegaljumps(pos, movelist);
    41.     if (n==0)
    42.     {
    43.         n = getlegalmoves(pos, movelist); '<-- ERROR OCCURS HERE
    44.     }
    45.  
    46.     for(i=0; i<n; i++)
    47.     {
    48.         pos.blacks = movelist[i].blacks;
    49.         pos.whites = movelist[i].whites;
    50.         pos.kings = movelist[i].kings;
    51.         pos.colour = abs(pos.colour - 3);
    52.                
    53.         value = -alphabeta(pos, depth-1, -beta, -localalpha);
    54.  
    55.         pos = localpos;
    56.  
    57.         bestvalue = max(value, bestvalue);
    58.  
    59.         if (bestvalue>=beta)
    60.         {
    61.             break;
    62.         }
    63.  
    64.         if (bestvalue>localalpha)
    65.         {
    66.             localalpha = bestvalue;
    67.         }
    68.     }
    69.    
    70.     return bestvalue;
    71. }
    72.  
    73.  
    74.  
    75.  
    76.  
    77.  
    78. int getlegalmoves(struct postype pos, struct movetype movelist[20])
    79. {
    80.     int i, j;
    81.     int totalmoves;
    82.     int sqrfrom, sqrto;
    83.  
    84.     totalmoves=0;
    85.  
    86.     if(pos.colour == BLACK)
    87.     {
    88.         for(i=0;i<32;i++)
    89.         {
    90.             sqrfrom=i;
    91.             if((pos.blacks & i) && !(pos.kings & i))
    92.             {
    93.                 for(j=0; j<movecounts[bp][sqrfrom]; j++)
    94.                 {
    95.                     sqrto = legalmoves[bp][sqrfrom][j];
    96.                     if (!(pos.blacks & sqrto) && !(pos.whites & sqrto))
    97.                     {
    98.                         // This is a valid move
    99.                         movelist[totalmoves].blacks = pos.blacks;
    100.                         movelist[totalmoves].whites = pos.whites;
    101.                         movelist[totalmoves].kings = pos.kings;
    102.                         movelist[totalmoves].from = sqrfrom;
    103.                         movelist[totalmoves].to = sqrto;
    104.                        
    105.                         totalmoves++;
    106.                     }
    107.                 }
    108.             }
    109.         }
    110.     }
    111.     return totalmoves;
    112. }

  6. #6
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    Why do you have 20 in the parameter list of your function? Try just [], not [20]

  7. #7

    Thread Starter
    Frenzied Member ae_jester's Avatar
    Join Date
    Jun 2001
    Location
    Kitchener Ontario Canada Earth
    Posts
    1,545
    I'm getting the same error with or without them in there.

    I'm also starting to think that something else 'might' be causing all this crap to happen........argh now i know why i hate c++

  8. #8

    Thread Starter
    Frenzied Member ae_jester's Avatar
    Join Date
    Jun 2001
    Location
    Kitchener Ontario Canada Earth
    Posts
    1,545
    I'm attaching my cpp file in case anyone has time to look at it. Maybe you can see something I don't.

    Thanks
    Attached Files Attached Files

  9. #9
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    What compiler are you using....never seen stdafx.h before....is that anything you made?

  10. #10
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    I searched my computer and found about 20 files called that...most of them in the DX8 SDK and one in mfc/src and one in atl/src


    Do you know what you are using?

  11. #11
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    Hehe...now I am having problems here....those folders are added to the directory list so the compiler should have no problem of finding them....stupid C++...

  12. #12
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    Hehehe...I moved the protoyupe at the top of the prototypes, and the function over the last one...and then everything worked.......no idea why tho'.....

  13. #13
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    Or actually I still get the

    C:\struct.cpp(468) : fatal error C1004: unexpected end of file found

    error....not sure what is causing that but I commented out the #include "STDAFX.H" becuase I have no idea why C++ could not find it...and I have no idea what you are using it for...so that may couase that error...I don't know...

  14. #14
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    And you have to initialise n in the alphabeta function....and ther is some error in the evaluation function...can't see the problem...looks like you have missed out a } but I can't find where....and it sais that not all paths have a return value....

  15. #15
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    FOUND IT....your BIG problem was that the evaluation function has two {{ in the last if test....so try to take away one of them and initialise n then you are ready to rumble....just ignore all other posts from me...


    [Edit]Now I have deserved the date I am on my way too....hitting the shower...happy C++ programming[/Edit]

  16. #16

    Thread Starter
    Frenzied Member ae_jester's Avatar
    Join Date
    Jun 2001
    Location
    Kitchener Ontario Canada Earth
    Posts
    1,545
    Ahhhhh

    I knew it would be something so stupid

    Thanks so much!

  17. #17

    Thread Starter
    Frenzied Member ae_jester's Avatar
    Join Date
    Jun 2001
    Location
    Kitchener Ontario Canada Earth
    Posts
    1,545
    I'm using Microsoft Visual C++ 6.0

    STDAFX.H was automatically included when I started a new project. I have no idea why either.

  18. #18
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    Originally posted by ae_jester
    Ahhhhh

    I knew it would be something so stupid

    Thanks so much!

    Your subject still says unresolved......was there something more you wondered about...??

  19. #19
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    Originally posted by ae_jester
    I'm using Microsoft Visual C++ 6.0

    STDAFX.H was automatically included when I started a new project. I have no idea why either.

    I am always starting with a clean project...so nothing else then what I want is added....but if you want it there no worries....even if I can't see whee you are using anything from it...

  20. #20

    Thread Starter
    Frenzied Member ae_jester's Avatar
    Join Date
    Jun 2001
    Location
    Kitchener Ontario Canada Earth
    Posts
    1,545
    NoteMe, do u know of any good forums for c++? This one is kinda 'slow' if you ask me.

  21. #21
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    Hehe...wasn't I fast enough.......no sorry....I havn't had time to look yet....I have only been doing C++ for 4 months....so this was more or less the first non school C++ app I have ever seen...

  22. #22
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    cboard.cprogramming.com is very good.

    Kinda loaded server though.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  23. #23
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    Thanks will check it out after X-Mas

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width