2016-10-27 4 views
-2

Jemand hat mir empfohlen, boost :: variant als Formvariable zu verwenden, um verschiedene Arten von Formen darin zu speichern. Aber, als boost :: variant in meinen Code implementiert wurde, kam es beim Kompilieren zu einem Fehler. Fehler sagt: 'Shape': Basisklasse undefiniert und mehr Fehler.Klassen in boost speichern :: Variante

Hier ist mein Code (Object.h):

using Shape = boost::variant<Rectangle, Circle>; 

enum Shape_Type 
{ 
    RECTANGLE, 
    CIRCLE 
}; 

struct Position 
{ 
    float x, y; 

    Position(float position_x, float position_y) 
    { 
     x = position_x; 
     y = position_y; 
    } 
}; 

class Object : private Shape 
{ 

private: 
    std::string name; 

public: 
    Object() = default; 

    Object(std::string name, Rectangle rectangle) : name(name), Shape(rectangle) 
    { 
    } 

    Object(std::string name, Circle circle) : name(name), Shape(circle) 
    { 
    } 

    void setPosition(float, float); 

    void setAngle(float); 

    Shape* getShape() 
    { 
     Shape* shape = this; 
     return shape; 
    } 

    Position getPosition(); 

    const std::string* getName() 
    { 
     return &name; 
    } 
}; 

class Rectangle 
{ 

private: 
    sf::RectangleShape rectangleshape; 

public: 
    Rectangle() = default; 

    Rectangle(float width, float height) 
     : rectangleshape(sf::RectangleShape(sf::Vector2f(width, height))) 
    { 
    } 

    void setPosition(float position_x, float position_y) 
    { 
     rectangleshape.setPosition(position_x, position_y); 
    } 

    void setAngle(float angle) 
    { 
     rectangleshape.setRotation(angle); 
    } 

    sf::RectangleShape* getRectangleShape() 
    { 
     return &rectangleshape; 
    } 

    Position getPosition() 
    { 
     return Position(rectangleshape.getPosition().x, 
         rectangleshape.getPosition().y); 
    } 
}; 

class Circle 
{ 

private: 
    sf::CircleShape circleshape; 

public: 
    Circle() = default; 

    Circle(std::string name, float radius) 
     : circleshape(sf::CircleShape(radius)) 
    { 
    } 

    void setPosition(float position_x, float position_y) 
    { 
     circleshape.setPosition(position_x, position_y); 
    } 

    void setAngle(float angle) 
    { 
     circleshape.setRotation(angle); 
    } 

    sf::CircleShape* getCircleShape() 
    { 
     return &circleshape; 
    } 

    Position getPosition() 
    { 
     return Position(circleshape.getPosition().x, 
         circleshape.getPosition().y); 
    } 
}; 

Und btw ist GetShape() Funktion gut?

Antwort

1

Varianten werden für statische Polymorphie verwendet, so dass Sie die Basisklasse überhaupt nicht benötigen (das ist dynamisch - oder virtuell - Polymorphismus).

Die Mitglieder in einer Variante typischerweise nicht über eine gemeinsame Basisklasse teilen, so dass Sie die getShape Funktion nicht haben würde, oder würden Sie es als Templat benötigen:

template <typename T> 
T const& getShape() const { return boost::get<T>(_shape); } 
Verwandte Themen