1

だから、私は循環依存に大きな問題を抱えています。私の Square.h と Circle.h クラスは両方とも Shape.h を継承し、2 つの間の衝突を検出しようとするために二重ディスパッチを使用します。私のクラスは現在、次のような方法でセットアップされています

シェイプ.h

class Shape {
public:
    virtual bool detectCollision(Shape* obj) = 0;
    virtual bool detectCollision(Square* obj) = 0;
    virtual bool detectCollision(Circle* obj) = 0;

Square.h

#include "shape.h"
class Square : public Shape {
public:
    bool detectCollision(Shape* obj);
    bool detectCollision(Square* obj);
    bool detectCollision(Circle* obj);

Circle.h

#include "shape.h"
class Circle: public Shape {
public:
    bool detectCollision(Shape* obj);
    bool detectCollision(Square* obj);
    bool detectCollision(Circle* obj);

基本的に、私は次のようなことができるようになりたいと思っています

Circle circle;
Square square;
Square square2;

circle.detectCollision(&square);
square.detectCollision(&square2);

しかし、これをコンパイルしようとすると、いくつかのエラーが発生します。明らかに「Circle.h」を含めて、Square.h 内で実行されない循環ループが発生します。誰かがこの問題の良い解決策を提案できますか?

明らかに、2 つの正方形と円と正方形の間の衝突検出は異なるため、何らかの方法でこれらのメソッドをオーバーロードする必要があります。これは良い解決策だと思いましたが、何か指針はありますか?

エラー (これらのコンパイル エラーは同じか、Square.cpp と Shape.cpp です):

Circle.cpp
    shape.h(12): error C2061: syntax error : identifier 'Square'
    shape.h(13): error C2061: syntax error : identifier 'Circle'
    shape.h(13): error C2535: 'bool Shape::detectCollision(void)' : member function already defined or declared
4

2 に答える 2