Home > Net >  E0298 inherited member is not allowed
E0298 inherited member is not allowed

Time:11-07

I am trying to program chess. I want create is virtual parent class Tool, and child classes for each peace type. Here is my 4 files I writed:

Tool.h

#pragma once
#include <iostream>
using namespace std;

class Tool
{
protected:
    string type;
    int row;
    int colum;
    int player;
public:
    Tool(int row, int col, int player = 0);
    string getType();
    int getRow();
    int getColum();
    int getPlayer();
    bool isLegitimateMove(int row, int col);
    void move(int row, int col);
};

Tool.cpp

#include "Tool.h"

Tool::Tool(int x, int y , int p) :
    type("")
{
    row = x;
    colum = y;
    player = p;
}

int Tool::getColum() {
    return colum;
}

int Tool::getRow() {
    return row;
}

int Tool::getPlayer() {
    return player;
}

string Tool::getType() {
    return type;
}

void Tool::move(int newRow, int newColum) {
    row = newRow;
    colum = newColum;
}

King.h

#pragma once
#include "Tool.h"
class King : public Tool {
};

King.cpp

#include "King.h"
#include <cstdlib>

bool King::isLegitimateMove(int a, int b) {
    return (abs(a - row) <= 1) and (abs(b - colum) <= 1);
}

But the VS don't give King to inherit from Tool and write the next errors:

E0298   inherited member is not allowed (King.cpp Line 4)
C2509   'isLegitimateMove': member function not declared in 'King' (King.cpp Line 4)

Can you help me fix this code? I have tyried read this manuals https://www.geeksforgeeks.org/inheritance-in-c/ but it didn't help me.

CodePudding user response:

The problem is that in order to provide an out of class definition for a member function of a class, a declaration for that member function must be present inside the class. And since there is no such declaration inside the derived class King, we can't define it that way you did.

Thus to solve this add a declaration for the member function inside the derived class King:

class King : public Tool {
    bool isLegitimateMove(int row, int col); //declaration added 
};

Also you might want to make isLegitimateMove a virtual member function by adding the keyword virtual when declaring it in the base class.

  • Related