Issues with classes

Discussion of chess software programming and technical issues.

Moderator: Ras

ZirconiumX
Posts: 1366
Joined: Sun Jul 17, 2011 11:14 am
Full name: Hannah Ravensloft

Issues with classes

Post by ZirconiumX »

OK - GCC 4.0.1 is giving me a strange error:

My code:
board.hpp:

Code: Select all

/*
 *  board.hpp
 *  Magic Chess
 *
 *  Created by Matthew Brades on 26/12/2011.
 *
 */

#ifndef BOARD_H
	#define BOARD_H
	#include <stdint.h>
	
typedef struct move {
	unsigned char	From,
					Dest,
					Piece,
					Colour,
					CapturedPiece,
					CapturedColour,
					Flags;
} move;

typedef struct moves {
	move Stack[100];
	int Value[100];
	int StackNumber;
} moves;

class board {
public:
	board(uint64_t = 0xFF00ULL,			uint64_t = 0x42ULL,					uint64_t = 0x24ULL,
			uint64_t = 0x81ULL,					uint64_t = 0x8ULL,					uint64_t = 0x10ULL,
			uint64_t = 0xFF000000000000ULL,		uint64_t = 0x4200000000000000ULL,	uint64_t = 0x8100000000000000ULL,
			uint64_t = 0x2400000000000000ULL,	uint64_t = 0x800000000000000ULL,	uint64_t = 0x1000000000000000ULL,
			uint64_t = 0ULL,					uint64_t = 0xFFFF00000000FFFFULL,	uint64_t = 0xFFFFFFFF0000ULL,
			uint64_t = 0xFFFF000000000000ULL,	uint64_t = 0xFFFFULL,				int = 0xFULL);

	board(const board&);

	void PrintBitboard(uint64_t);
	
	bool MakeMove(move);
	bool MakeCapture(move);
	void UnmakeMove(move);
	void UnmakeCapture(move);

private:
	uint64_t	Pieces[12],
				Colours[2],
				Enpassant,
				Occupied,
				Empty;
	int			CastlingRights,
				SideToMove,
				Halfmove,
				Fullmove;
} board;

#endif
board.cpp

Code: Select all

/*
 *  board.cpp
 *  Magic Chess
 *
 *  Created by Matthew Brades on 26/12/2011.
 *
 */

#include "board.hpp"
#include <cstdio>

board::board(uint64_t whitePawns, uint64_t whiteKnights, uint64_t whiteBishops,
		uint64_t whiteRooks, uint64_t whiteQueens, uint64_t whiteKing,
		uint64_t blackPawns, uint64_t blackKnights, uint64_t blackBishops,
		uint64_t blackRooks, uint64_t blackQueens, uint64_t blackKing,
		uint64_t enpassant, uint64_t occupied, uint64_t empty,
		uint64_t black, uint64_t white, int castlingRights)
{
	Pieces[0]		= whitePawns;
	Pieces[1]		= whiteKnights;
	Pieces[2]		= whiteBishops;
	Pieces[3]		= whiteRooks;
	Pieces[4]		= whiteQueens;
	Pieces[5]		= whiteKing;
	Pieces[6]		= blackPawns;
	Pieces[7]		= blackKnights;
	Pieces[8]		= blackBishops;
	Pieces[9]		= blackRooks;
	Pieces[10]		= blackQueens;
	Pieces[11]		= blackKing;
	Enpassant		= enpassant;
	Occupied		= occupied;
	Empty			= empty;
	Colours[0]		= white;
	Colours[1]		= black;
	CastlingRights	= castlingRights;
}

board::board(const board& Board)
{
	for (int i = 0; i < 12 && (Pieces[i] = Board.Pieces[i]); i++)
		;
	Enpassant		= Board.Enpassant;
	Occupied		= Board.Occupied;
	Empty		= Board.Empty;
	for (int i = 0; i < 2 && (Colours[i] = Board.Colours[i]); i++)
		;
	CastlingRights	= Board.CastlingRights;
}

void PrintBitboard(uint64_t b)
{
	printf("%llu\n", b);
}

bool board::MakeMove(move m)
{
	uint64_t a, b, c;
	a = 1ULL << m.From;
	b = 1ULL << m.Dest;
	c = a ^ b;
	Pieces[m.Piece]		^= c;
	Colours[m.Colour]	^= c;
	Occupied			^= c;
	Empty				^= c;
	
	return true;
}
	
	
bool board::MakeCapture(move m)
{
	uint64_t a, b, c;
	a = 1ULL << m.From;
	b = 1ULL << m.Dest;
	c = a ^ b;
	Pieces[m.Piece]				^= c;
	Colours[m.Colour]			^= c;
	Pieces[m.CapturedPiece]		^= b;
	Colours[m.CapturedColour]	^= b;
	Occupied					^= a;
	Empty						^= a;
	
	return true;
}

void board::UnmakeMove(move m)
{
	uint64_t a, b, c;
	a = 1ULL << m.From;
	b = 1ULL << m.Dest;
	c = a ^ b;
	Pieces[m.Piece]		^= c;
	Colours[m.Colour]	^= c;
	Occupied			^= c;
	Empty				^= c;
}

void board::UnmakeCapture(move m)
{
	uint64_t a, b, c;
	a = 1ULL << m.From;
	b = 1ULL << m.Dest;
	c = a ^ b;
	Pieces[m.Piece]				^= c;
	Colours[m.Colour]			^= c;
	Pieces[m.CapturedPiece]		^= b;
	Colours[m.CapturedColour]	^= b;
	Occupied					^= a;
	Empty						^= a;
}
Now when I have main.cpp as

Code: Select all

/*
 *  main.cpp
 *  My Bitboard Project
 *
 *  Created by Matthew Brades on 26/12/2011.
 *
 */
#include "board.hpp"

int main()
{
	board x; // expected ';' before 'x'
	return 0;
}
I get the error in the comment.

When I try it with the class keyword in front of it, it gives me a linker failure (which is understandable)

What am I doing wrong?

Matthew:out
tu ne cede malis, sed contra audentior ito
Sven
Posts: 4052
Joined: Thu May 15, 2008 9:57 pm
Location: Berlin, Germany
Full name: Sven Schüle

Re: Issues with classes

Post by Sven »

ZirconiumX wrote:OK - GCC 4.0.1 is giving me a strange error:

My code:
board.hpp:

Code: Select all

typedef struct move {
...
} move;

typedef struct moves {
...
} moves;

class board {
...
} board;
Now when I have main.cpp as

Code: Select all

...
#include "board.hpp"

int main()
{
	board x; // expected ';' before 'x'
	return 0;
}
I get the error in the comment.

When I try it with the class keyword in front of it, it gives me a linker failure (which is understandable)

What am I doing wrong?
In board.hpp write this to remove the error:

Code: Select all

class board {
...
};
Then, in C++ you can also write this to keep everything as simple as possible:

Code: Select all

struct move {
...
};

struct moves {
...
};
No need for the weird "typedef" for structs to be able to use the struct name without the keyword "struct".

Note that, unlike C, in C++ a "struct" is the same as a "class" with the exception that all members are public and you can't use the "private/protected/public" keywords therein.

Sven
ZirconiumX
Posts: 1366
Joined: Sun Jul 17, 2011 11:14 am
Full name: Hannah Ravensloft

Re: Issues with classes

Post by ZirconiumX »

Sven,

I need the typedef keywords,
  1. because I am lazy.
  2. because I use them a types (if you read the prototypes for MakeMove etc.) you will find that they are used as types.
  3. but mostly because it is how I code.
Thanks for the quick reply!

Matthew:out
tu ne cede malis, sed contra audentior ito
Sven
Posts: 4052
Joined: Thu May 15, 2008 9:57 pm
Location: Berlin, Germany
Full name: Sven Schüle

Re: Issues with classes

Post by Sven »

ZirconiumX wrote:Sven,

I need the typedef keywords,
  1. because I am lazy.
  2. because I use them a types (if you read the prototypes for MakeMove etc.) you will find that they are used as types.
  3. but mostly because it is how I code.
Thanks for the quick reply!
No, you don't need them for b when using C++.

Sven
mcostalba
Posts: 2684
Joined: Sat Jun 14, 2008 9:17 pm

Re: Issues with classes

Post by mcostalba »

ZirconiumX wrote:OK - GCC 4.0.1 is giving me a strange error:

My code:
board.hpp:

Code: Select all

class board {
.....
} board;

The code above defines a class called board and a variable of that calss (an object) called again board.

You have the error because the compiler interprets the 'board' in:

Code: Select all

board x;
as the variable name, not the class name.

I suggest to write as follwoing

Code: Select all

class Board {
.....
};

extern Board board;

the extern declaration is needed if you are going to include board.hpp in multiple files to avoid a "variable redefinition" error.


....and now back to try to fix SF time management issue ! :-)