Problems with constructors

Hi

I have a constructor for a class that takes an argument:

chessBoard( bool StandardBoard )

{

// reserve memory for the array variables

boardContents = gcnew array<unsigned char>(64);

KSCastle = gcnew array<bool>(2);

QSCastle = gcnew array<bool>(2);

whitePositions = gcnew array<unsigned char>(16);

blackPositions = gcnew array<unsigned char>(16);

//we want the board to appear empty to the program, so

//we fill all the squares with the empty value

for(int scrL = 0; scrL<64;scrL++)

boardContents[scrL] = 255;

if(StandardBoard) //commonly, the starting layout may be required.

{

//black pawns

for(int scrL=8; scrL<16; scrL++)

boardContents[scrL] = 6;

//white pawns

for(int scrL=48; scrL<56; scrL++)

boardContents[scrL] = 0;

//other pieces

//rooks

boardContents[0] = 9; //black

boardContents[7] = 9;

boardContents[56] = 3; // white

boardContents[63] = 3;

//knights

boardContents[1] = 7; //black

boardContentsDevil = 7;

boardContents[57] = 1; //white

boardContents[62] = 1;

//bishops

boardContents[2] = 8; //black

boardContents[5] = 8;

boardContents[58] = 2; //white

boardContents[61] = 2;

//queens

boardContents[3] = 10; //black

boardContents[59] = 4; //white

//kings

boardContents[4] = 11; //black

boardContents[60] = 5; //white

SetupPositions();

}

}

I have no other constructor defined. I am trying to implement a routine called CopyBoard so that I can copy my class exactly (with shallow copies of the arrays) by writing chessBoard newBoard = oldboard.CopyBoard;

I started writing it simply like this (within the body of the class):

//ATTEMPT AT A COPY ROUTINE //

chessBoard CopyBoard()

{

chessBoard newBoard = gcnew chessBoard( false );

return newBoard;

}

But when I compiled it (to make sure syntax etc. I get this error:

Error 1 error C2664: 'Representations::chessBoard::chessBoard(bool)' : cannot convert parameter 1 from 'Representations::chessBoard ^' to 'bool' Representations.h 112

I also notice that Intellisense thinks that chessBoard has two constructors. One is chessBoard( ) and the other is chessBoard ( const chessBoard & ) which I assume is the default copy constructor. Neither is the constructor that I actually defined.

This problem also occurs if I try to write the copy routine outside the class body.

Hope you can help!

Fritz



Answer this question

Problems with constructors

  • Rafael Cabedo

    Declare CopyBoard() like chessBoard& CopyBoard()

    The way you originally wrote it, you return a copy of a chessBoard object but there is no copy constructor. A copy constructor is highly recommended.



  • Suscuaja

    That worked! Bloody obvious, too! :)

    Many, many thanks!

    Fritz


  • Johan van Toll

    This throws the compile error:

    Error 1 error C3699: '&' : cannot use this indirection on type 'Representations::chessBoard' Representations.h 111

    Considering what I'm trying to achieve, could someone let me know how they would go about it, with some code examples

    Sorry about this - one of those stupid things where you get stuck on something simple, and need another pair of eyes to point out your mistake!

    Cheers

    Fritz


  • MobilTech

    Shouldn't you do:

    chessBoard^ CopyBoard()
    {
       chessBoard^ newBoard = gcnew chessBoard( false );
       return newBoard;
    }


  • Problems with constructors