I'm trying to write a solution to the 8 queens problem. I am trying to start with a empty board. I keep getting the following error "error C2664: 'SetQueen' : cannot convert parameter 1 from 'int' to 'int [][8]'
Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast" It's from the line SetQueen(chessboard[8][8], 0);
If anybody can tell me what I am doing wrong, I would greatly appreciate it. I'm just getting to arrays so I may not have declared them correctly either. Thanks for any assistance in helping me understand what I am trying to do.

#include "stdafx.h"
#include <iostream>

using std::cout;
using std::endl;

void SetQueen (int [8][8], int);
bool CheckAttack (int [8][8], int, int);
void DisplayBoard (int [8][8]);
int main()
{
	int chessboard[8][8];

	SetQueen(chessboard[8][8], 0);
	return 0;
}

void SetQueen (int chessboard[8][8], int column)
{
	for (int row=0; row<=7; row++)
		if (CheckAttack(chessboard,row,column))
		{	
			chessboard[row][column] = true;
			if (column==7)		
			{										
				DisplayBoard(chessboard);					
			}
			else SetQueen(chessboard, column+1);
			chessboard[row][column] = false;	
		}
}

bool CheckAttack (int chessboard[8][8], int testRow, int testColumn)
{
	for (int row = testRow; row >= 0; row--)
		for (int column = testColumn; column >= 0; column--)
			if (chessboard[row][column])
				return false;
	for (int column = testColumn; column >= 0; column--)
		if (chessboard[testRow][column])
			return false;
	for (int row = testRow; row <= 7; row++)
		for (int column = testColumn; column >= 0; column--)
			if (chessboard[row][column])
				return false;
	return true;
}



void DisplayBoard (int chessboard[8][8])
{
	for (int row = 0; row < 8; row++)
	{
	  for (int column = 0; column < 8; column++)
	  {
	    chessboard[row][column] = 0;
		cout << chessboard[row][column] << ' ';
	  }
	   cout << endl;
	}
}

In your call to the DetQueen( ) function, you must pass the array (actually, the starting address of the array.) You are in fact passing it a single element, an int, when you put two index values after the array name in your call.

int main()
{
int chessboard[8][8];

SetQueen(chessboard[8][8], 0);  //your incorrect usage
SetQueen( chessboard, 0 );  //correct usage
return 0;
}

When passing an array in its entirety to a function, just put the name in your call. Nothing in [].

In fact, you're passing a value that is outside the memory allocated to the array. Remember, for an array of size 8x8, the last valid index is [7][7].

Thanks, I made the change and now I don't get any errors however, my chessboard is completely blank. Can you look at my displayBoard code and tell me if I forgot anything?

Display board is setting every element to zero, then displaying - an empty board. It should test the content of each cell and display something to indicate empty or occupied.

But the real problem is in the SetQueen function. You're using a recursive function that does not seem to have a base case - how does it know to stop calling itself more?

This article has been dead for over six months. Start a new discussion instead.