2016-11-14 3 views
0
nicht erlaubt

Ich versuche, eine abstrakte Klasse TileMapView.h genanntObjekt der abstrakten Klasse Typ „DiamondMap“ ist

#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; 

}; 

ich diese abstrakte Klasse an DiamondMap .h und CPP hatte zu verwenden implementiert, aber wenn Ich bin ein neues Objekt instant Ich erhalte die Nachricht: (Objekt der abstrakten Klassenart "DiamondMap" ist nicht erlaubt: reine virtuelle Funktion "TileMapView :: computeDrawPosition" hat keinen Overrider). Dies geschieht mit den drei Methoden (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(); 

Antwort

1

solange man drei rein virtuelle Funktionen in bas Klasse deklarieren Sie haben sie in abgeleiteten Klasse zu implementieren.

In Ihrem Beispiel haben Sie sie nicht korrekt implementiert, da ihre Implementierung die gleiche Signatur (Anzahl der Parameter und Art der Parameter) erfordert, aber nicht in Ihrer Basisklasse. ComputeDrawPosition() hat den dritten Parameter als int (int tw), während in der abgeleiteten Sie es als Schwimmer so computeDrawPosition in der Kinderklasse haben implementiert eine andere Elementfunktion nicht die Basis ein:

//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){}; 
  • auch Basiselement Funktionen Sie diese konstante, während machen in abgeleiteten ist nicht konstant.

  • Ihr Problem Ihr Kind Klasse Schnittstelle zu lösen wie folgt aussieht:

    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{}; 
    
+0

Sie sind nicht verpflichtet, Schlagwort überschreiben, aber schauen Sie sich tief in Ihrem Basis-Header, wie diese Signaturen von reinen virtuellen Funktionen sind und in Ihrem abgeleiteten – Raindrop7

-1

Wollen Sie nicht die Hörner als const markieren ??

auch ein Stichwort Überschreibung am Ende des Funktionsprototyps in Diamant-Klasse hinzufügen in der Kompilierung einen Fehler zu sehen

+0

Ja ich das versucht habe, aber ich erhalte die Meldung: „Leere computeDrawPosition (int col, int row, float tw , float th, float & targetx, float & targety) "überschreibt" kein Basisklassenmitglied "für die drei Methoden, die ich zu überschreiben versuchte. –

+0

Die Funktionsüberschreibungen in der abgeleiteten Klasse müssen als const gekennzeichnet werden, wie Yashwanth erwähnt. – MarcD

+1

Warum habe ich Stimmen bekommen :( –

Verwandte Themen