I refer to this website a lot, and there seems to be a lot of very helpful support from its members, so I decided to join the forum. I am currently making my own solution to an n queens problem and ran into a snag with some of my class member functions.
First of all, I know that this is not the best way to do this, but it's the first time I've taken a stab at an n queens problem.
Here are my function definitions:
Code:
Queen::Queen()
{
queenLoc = new s_type[8];
//Creates array for queens with generic value (until input received)
numQueens = 8;
place = 0;
success() == 0;
}
Queen::~Queen()
{
delete []queenLoc;
}
Queen::Queen(int boardsize)
{
queenLoc = new s_type[boardsize];
numQueens = boardsize; //Takes user input, makes it size of board
place = 0;
placement();//Begins placing queens...
}
void Queen::push(s_type s)
{
if(success() == 0) //Will only push to stack if no solution yet
{
queenLoc[place] = s; //Pushes location of queen to stack
place++; //Increments number of places used by 1
return;
}
else if(success() == 1)
cout << "There is a solution!" << endl;
else
cout << "An error has occurred..." << endl;
//This final else statement is included for error checking
}
Queen::s_type Queen::pop()
{
s_type topqueenLoc;
if(counter() == 0)//Prevents popping from empty stack
{
return(topqueenLoc);
}
topqueenLoc = queenLoc[--place];
return(topqueenLoc);
}
void Queen::placement()
{
while(success() == 0 && !counter() == (numQueens))
{
if(conflict() == 1)
{
if(queenLoc[place] >= numQueens)
{
pop();
}
while(queenLoc[place] < numQueens)
{
queenLoc[place]++;
}
}
else if(conflict() == 0 && counter() == (numQueens))
{
success() == 1; //Calls suceess to return 1
printQueens(); //Calls function to print queens
}
else
{
push(1);
}
}
void Queen::printQueens()
{
int i;
for(i = 0; i <= numQueens; i++)
cout << "Queen row " << i << ": column " << queenLoc[i] << endl;
}
int Queen::counter()
{
return(place);
}
bool Queen::conflict()
{
int i,j;
for(i = 0; i <= numQueens; i++)
{
for(j = 1; j <= place; j++)
{
if(queenLoc[j] == i || (abs(queenLoc[j]-i) == (abs(j-place)))
return 0;
}
}
return 1;
}
bool Queen::success()
{
if(counter() == (numQueens))
return 1; //Returns 1 if all queens have been placed without conflict
else if(counter() != (numQueens))
return 0; //Returns 0 if a solution has not been found
else
cout << "A strange error has occurred..." << endl;
//I incorporated this else statement as a means of error checking,
//if this is ever triggered then I know something is wrong with my code...
}
The problem lies in the printQueens(), counter(), conflict(), and success() functions. My compiler (Dev C++) is kicking the following errors at me for each of them:
"expected primary-expression before "void (or int, or bool, respectively)" "
and
"expected `;' before "void (or int, or bool, respectively""
I know it's a very simple mistake, and I can't, for the life of me, figure this out! Any help is greatly appreciated, and I'll keep churning away at it...