Takashi Miyabe Takashi Miyabe - 25 days ago 8
C++ Question

object of abstract class type "DiamondMap" is not allowed

I'm trying to use an abstract class called TileMapView.h

#pragma once
class TileMapView
{

public:
TileMapView();
~TileMapView();
virtual void computeDrawPosition(const int col, const int row, const int tw, const int th, float &targetx, float &targety) const = 0;
virtual void computeMouseMap(int &col, int &row, const int tw, const int th, const int mx, const int my) const = 0;
virtual void tileWalking(int &col, int &row, unsigned char direction) const = 0;

protected:
int width, height;
int posX, posY, currentX, currentY;

};


I had implemented this abstract class at DiamondMap .h and .cpp, but when I'm instancing a new object I receive the message: (object of abstract class type "DiamondMap" is not allowed: pure virtual function "TileMapView::computeDrawPosition" has no overrider). This happens to the three methods (computeDrawPosition, computeMouseMap, tileWalking).

DiamondMap.h

#pragma once
#include "TileMapView.h"

class DiamondMap: public TileMapView
{
public:
DiamondMap();
~DiamondMap();
void computeDrawPosition(const int col, const int row, const float tw, const float th, float &targetx, float &targety);
void tileWalking(int &col, int &row, unsigned char direction);
void computeMouseMap(int &col, int &row, const int tw, const int th, const int mx, const int my);
};


main.cpp

#include <Windows.h>
#include <GL/gl.h>
#include <GL/glut.h>
#include "DiamondMap.h"

TileMapView *view;
...
view = new DiamondMap();

Answer

as long as you declare three pure virtual functions in bas class you have to implement them in derived class.

in your example you didn't implement them correctly because implementing them requires providing the same signature (number of parameters and type of parameters) but you didn't for example in your base class computeDrawPosition() has the third parameter as int (int tw) while in derived you have it as float thus computeDrawPosition in child class is another member function not implementing the base one:

//in base:
void computeDrawPosition(const int col, const int row, const int tw, const int th, float &targetx, float &targety) const = 0;

//in derived:
void computeDrawPosition(const int col, const int row, const float tw, const float th, float &targetx, float &targety){};
  • also you make base member functions' this constant while in derived is not constant.

  • to solve your problem your child class interface looks like:

    void computeDrawPosition(const int col, const int row, const int tw, const int th, float &targetx, float &targety) const {}
    void computeMouseMap(int &col, int &row, const int tw, const int th, const int mx, const int my) const{}
    void tileWalking(int &col, int &row, unsigned char direction) const{};