Reputation:
I'm been getting this error and I'm not sure how to fix it, #include "stdafx.h"
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
class Puzzle {
public:
virtual bool action(char [][8], int, int) = 0;
virtual void print_solution(char [][8], int) = 0;
};
class Queen8: public Puzzle {
public:
bool action(char Q[][8], int row, int col) {
for (int r = 0; r < row; r++) {
if (Q[r][col] == '1') {
return false;
}
}
for (int r = row, c = col; r >= 0 && c >= 0; r--, c--) {
if (Q[r][c] == '1') {
return false;
}
}
for (int r = row, c = col; r >= 0 && c < 8; r--, c++) {
if (Q[r][c] == '1') {
return false;
}
else {
return true;
}
}
}
void print_solution(char Q[][8], int row) {
if (row == 8)
{
for (int r = 0; r < 8; r++) {
for (int c = 0; c < 8; c++)
cout << Q[r][c] << " ";
cout << endl;
}
cout << endl;
return;
}
for (int c = 0; c < 8; c++) {
if (action(Q, row, c)) {
Q[row][c] = '1';
print_solution(Q, row + 1);
Q[row][c] = '0';
}
}
}
};
int main() {
Puzzle Queen8;
char Q[8][8];
for (int r = 0; r < 8; r++) {
for (int c = 0; c < 8; c++) {
Q[r][c] = '0';
}
}
Queen8.print_solution(Q, 0);
}
The exact error is:
c:\users\delta\onedrive\documents\visual studio 2013\projects\consoleapplication46\consoleapplication46\consoleapplication46.cpp(60): error C2259: 'Puzzle' : cannot instantiate abstract class
1> due to following members:
1> 'bool Puzzle::action(char [][8],int,int)' : is abstract
1> c:\users\delta\onedrive\documents\visual studio 2013\projects\consoleapplication46\consoleapplication46\consoleapplication46.cpp(9) : see declaration of 'Puzzle::action'
1> 'void Puzzle::print_solution(char [][8],int)' : is abstract
1> c:\users\delta\onedrive\documents\visual studio 2013\projects\consoleapplication46\consoleapplication46\consoleapplication46.cpp(10) : see declaration of 'Puzzle::print_solution'
Upvotes: 0
Views: 307
Reputation: 4884
In your main()
function you are instantiating the Puzzle
class, not the Queen8
class:
Puzzle Queen8;
You should instantiate the Queen8
instead:
Queen8 q;
...
q.print_solution(Q, 0);
Besides that, you should always use the override
keyword when you override virtual functions (C++11 and beyond). This will tell the compiler your intention and it will give you much better warnings and errors in case you miss anything. So inside the Queen8
class, you should have:
bool action(char Q[][8], int row, int col) override {
...
}
void print_solution(char Q[][8], int row) override {
...
}
Upvotes: 3