Thread: Class data being corrupted after passing it through a paramater???

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    17

    Class data being corrupted after passing it through a paramater???

    I've spent hours narrowing my problem down to these few lines of code.

    This is how my two dimensional array of classes is defined:
    Code:
    square board[8][8];   //Creates a 8x8 array of square objects
    This is the function I'm using to change the array's elements.
    Code:
        void setBoard(square b[8][8])
        {
           int j;
           vector <CP> wPawn(8,'W'); //defines a vector of 8 CP objects
           vector <CP> bPawn(8,'B');
           for (j=0;j<8;j++)
           {
              b[j][1].place(&wPawn[j]);
              b[j][6].place(&bPawn[j]);   
           }
           cout << b[1][1];                //tests this individual square's data : Outputs correctly (the char 'P')
           pause();                            //similar to a system pause
        }
    Where the strangeness comes in:
    Code:
          setBoard(board);             //the function above   
           cout << board[1][1];       //tests this individual square's data : Outputs a seemingly random character ('=' O_o)
           pause();                          //similar to a system pause
    When I output the whole array, the unchanged square objects output correctly. However, the ones that are changed inside of setBoard() become corrupted or something. Is there anything wrong with the way I'm passing that parameter? I've gotten away with doing it like this before.

  2. #2
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    I have to think that it has something to do with your vectors being destroyed once the function is finished.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > b[j][1].place(&wPawn[j]);
    What's a square class do?

    This looks suspiciously like saving a pointer to a local variable to me.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Just kidding.... fnoyan's Avatar
    Join Date
    Jun 2003
    Location
    Still in the egg
    Posts
    275
    If you want to change the value of a variable in a function, you have to pass a pointer to that function or a reference in C++.

  5. #5
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    If you want to write a competent chess program, why use classes at all. Use bitboards, or 0x88 boards, or use classes to represent entire boards (this at least makes more sense to me). If you use piles of abstraction, your program would probably do no better than 8 ply using the best algorithms even on Deep Blue hardware, for instance. (If you're working on another senseless class assignment or not writing any AI at all, well you can ignore this at your own peril.)

    system("pause") is doubleplusungood, use cin.get() while you have it.

    Why not

    Code:
    enum piece_type
    {
        empty, bpawn, wpawn, //etc
    };
    
    class board
    {
        protected:
            piece_type board_data[8][8];
        public:
            //stuff...
    }
    Last edited by jafet; 10-28-2006 at 07:14 AM.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  6. #6
    Registered User
    Join Date
    Oct 2006
    Posts
    17
    I see what the problem is. Once the function is finished it deletes every local variable, including the vectors. I guess I was too tired to realize that last night.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with FIFO QUEUE
    By jackfraust in forum C++ Programming
    Replies: 23
    Last Post: 04-03-2009, 08:17 AM
  2. xor linked list
    By adramalech in forum C Programming
    Replies: 23
    Last Post: 10-14-2008, 10:13 AM
  3. Bitmasking Problem
    By mike_g in forum C++ Programming
    Replies: 13
    Last Post: 11-08-2007, 12:24 AM
  4. singly linked circular list
    By DarkDot in forum C++ Programming
    Replies: 0
    Last Post: 04-24-2007, 08:55 PM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM