1

アプリケーション用に 3 つのクラスを定義しています: int2_(整数のカップル)、float2_(浮動小数点のカップル)、およびdouble2_(倍精度のカップル) は、本質的に複雑な算術演算を実行するためのものです。

=上記の 3 つのクラスのオブジェクト間の代入演算子をオーバーロードしたいと考えています。動機は、私が CUDA コードを書いていることですが、CUDA クラスint2float2およびに対して既に定義されている代入演算子がないようですdouble2

私の現在の実装は次のとおりです。

class int2_ {

    public:
        int x;
        int y;

        int2_() : x(), y() {}

        // Stuff
        const int2_& operator=(const float2_ a)     { int2_ b;  b.x = (int)a.x;             b.y = (int)a.y;     return b; }
        const int2_& operator=(const double2_ a)    { int2_ b;  b.x = (int)a.x;             b.y = (int)a.y;     return b; }
};

class float2_ {

    public:
        float x;
        float y;

        float2_() : x(), y() {}

        // Stuff
        const float2_& operator=(const double2_ a)  { float2_ b;    b.x = (float)a.x;       b.y = (float)a.y;   return b; }
};

class double2_ {

    public:
        double x;
        double y;

        double2_() : x(), y() {}

        // Stuff
};

stuffはコンパイルエラーを提供しないため、すべてが要約されています。残りの演算子のオーバーロードは、次のエラーを返します

error : identifier "float2_" is undefined
error : identifier "double2_" is undefined  
error : identifier "double2_" is undefined  

3 つの異なるオーバーロードに対してそれぞれ。

たとえば、クラス定義の前にまだ定義されていないfloat2_という事実により、「可視性」の問題があるようです。問題を解決する方法について何か提案はありますか? 事前にどうもありがとうございました。double2_int2_

JAMES KANZE と ALEXRIDER の回答に従うソリューション

解決策は次のとおりです。

class int2_;
class float2_;
class double2_;

class int2_ {

    public:
        int x;
        int y;

        int2_() : x(), y() {}

        inline const int2_& operator=(const int a)          { x = a;            y = 0.;         return *this; }
        inline const int2_& operator=(const float a)        { x = (int)a;       y = 0.;         return *this; }
        inline const int2_& operator=(const double a)       { x = (int)a;       y = 0.;         return *this; }
        inline const int2_& operator=(const int2_ a)        { x = a.x;          y = a.y;        return *this; }
        inline const int2_& operator=(const float2_ a);
        inline const int2_& operator=(const double2_ a);
};

class float2_ {

    public:
        float x;
        float y;

        float2_() : x(), y() {}

        inline const float2_& operator=(const int a)        { x = (float)a;     y = 0.;         return *this; }
        inline const float2_& operator=(const float a)      { x = a;            y = 0.;         return *this; }
        inline const float2_& operator=(const double a) { x = (float)a;     y = 0.;         return *this; }
        inline const float2_& operator=(const int2_ a)      { x = (float)a.x;   y = (float)a.y; return *this; }
        inline const float2_& operator=(const float2_ a)    { x = a.x;          y = a.y;        return *this; }
        inline const float2_& operator=(const double2_ a);
};

class double2_ {

    public:
        double x;
        double y;

        double2_() : x(), y() {}

        inline const double2_& operator=(const int a)       { x = (double)a;    y = 0.;         return *this; }
        inline const double2_& operator=(const float a) { x = (double)a;    y = 0.;         return *this; }
        inline const double2_& operator=(const double a)    { x = a;            y = 0.;         return *this; }
        inline const double2_& operator=(const int2_ a) { x = (double)a.x;  y = (double)a.y;return *this; }
        inline const double2_& operator=(const float2_ a)   { x = (double)a.x;  y = (double)a.y;return *this; }
        inline const double2_& operator=(const double2_ a)  { x = a.x;          y = a.y;        return *this; }

};

        inline const int2_& int2_::operator=(const float2_ a)       { x = (int)a.x;             y = (int)a.y;       return *this; }
        inline const int2_& int2_::operator=(const double2_ a)      { x = (int)a.x;             y = (int)a.y;       return *this; }
        inline const float2_& float2_::operator=(const double2_ a)  { x = (float)a.x;           y = (float)a.y;     return *this; }
4

3 に答える 3