我是否需要在用户定义类型中重新定义成员选择器运算符?

时间:2012-06-06 06:58:08

标签: visual-studio-2008 c++-cli operator-overloading

我可能会在这里做些蠢事,我想要一个指针(原谅双关语)。

我定义了以下类:

ref class CoordinatePair {
public:
    int x;
    int y;
    CoordinatePair();
    CoordinatePair(int xInput, int yInput);
    CoordinatePair(CoordinatePair ^Other);
    //CoordinatePair& operator->();
};

相当简单。我发现我可以在类的命名空间中使用->运算符选择成员而不会产生任何不良影响。例如,以下编译:

CoordinatePair::CoordinatePair(CoordinatePair ^Other) {
    x = Other->x;
    y = Other->y;
}

Groovy的。然而,当我尝试编译时,我遇到了问题。

CoordinatePair^ Coordinates::TranslateCoords(CoordinatePair^ WorldCoords) {
    CoordinatePair^ newCoords = gcnew CoordinatePair();
    float coordsRatio = 0.0;
    //Translate X
    coordsRatio = (float) WorldCoords->x / WorldBounds->x;
    newCoords->x = (int) (coordsRatio * PixelBounds->x);
    //Translate Y
    coordsRatio = 0.0;
    coordsRatio = (float) WorldCoords->y / WorldBounds->y;
    newCoords->y = (int) (coordsRatio * PixelBounds->y);
    return newCoords;
}

(注意,在上面的代码中,WorldBoundsCoordinates类的成员。它本身是CoordinatePair,用于定义项目的平面。)

具体来说,我得到了这个错误:

.\Coordinates.cpp(95) : error C2819: type 'CoordinatePair' does not have an overloaded member 'operator ->'

咦。哦,那好吧。我试图研究这个问题促使我尝试重载运算符。所以,我在类声明中添加了以下内容:

CoordinatePair^ operator->();

我的定义如下:

CoordinatePair^ CoordinatePair::operator->() {
    return this;
}

这使编译器更加愤怒!: - (

.\Coordinates.cpp(17) : error C2818: application of overloaded 'operator ->' is recursive through type 'CoordinatePair'
        y:\documents\wende-project\c3\wende_c3_executioncode\wende_c3_executioncode\c3_display\c3_app\Coordinates.h(7) : see declaration of 'CoordinatePair'
.\Coordinates.cpp(17) : warning C4280: 'operator ->' was self recursive through type 'CoordinatePair'
        y:\documents\wende-project\c3\wende_c3_executioncode\wende_c3_executioncode\c3_display\c3_app\Coordinates.h(7) : see declaration of 'CoordinatePair'
.\Coordinates.cpp(18) : error C2818: application of overloaded 'operator ->' is recursive through type 'CoordinatePair'
        y:\documents\wende-project\c3\wende_c3_executioncode\wende_c3_executioncode\c3_display\c3_app\Coordinates.h(7) : see declaration of 'CoordinatePair'
.\Coordinates.cpp(18) : warning C4280: 'operator ->' was self recursive through type 'CoordinatePair'
        y:\documents\wende-project\c3\wende_c3_executioncode\wende_c3_executioncode\c3_display\c3_app\Coordinates.h(7) : see declaration of 'CoordinatePair'
.\Coordinates.cpp(62) : error C2818: application of overloaded 'operator ->' is recursive through type 'CoordinatePair'
        y:\documents\wende-project\c3\wende_c3_executioncode\wende_c3_executioncode\c3_display\c3_app\Coordinates.h(7) : see declaration of 'CoordinatePair'
.\Coordinates.cpp(62) : warning C4280: 'operator ->' was self recursive through type 'CoordinatePair'
        y:\documents\wende-project\c3\wende_c3_executioncode\wende_c3_executioncode\c3_display\c3_app\Coordinates.h(7) : see declaration of 'CoordinatePair'
.\Coordinates.cpp(62) : error C2818: application of overloaded 'operator ->' is recursive through type 'CoordinatePair'
        y:\documents\wende-project\c3\wende_c3_executioncode\wende_c3_executioncode\c3_display\c3_app\Coordinates.h(7) : see declaration of 'CoordinatePair'
.\Coordinates.cpp(62) : warning C4280: 'operator ->' was self recursive through type 'CoordinatePair'
        y:\documents\wende-project\c3\wende_c3_executioncode\wende_c3_executioncode\c3_display\c3_app\Coordinates.h(7) : see declaration of 'CoordinatePair'
.\Coordinates.cpp(62) : error C2818: application of overloaded 'operator ->' is recursive through type 'CoordinatePair'
        y:\documents\wende-project\c3\wende_c3_executioncode\wende_c3_executioncode\c3_display\c3_app\Coordinates.h(7) : see declaration of 'CoordinatePair'
.\Coordinates.cpp(62) : warning C4280: 'operator ->' was self recursive through type 'CoordinatePair'
        y:\documents\wende-project\c3\wende_c3_executioncode\wende_c3_executioncode\c3_display\c3_app\Coordinates.h(7) : see declaration of 'CoordinatePair'
.\Coordinates.cpp(62) : error C2818: application of overloaded 'operator ->' is recursive through type 'CoordinatePair'
        y:\documents\wende-project\c3\wende_c3_executioncode\wende_c3_executioncode\c3_display\c3_app\Coordinates.h(7) : see declaration of 'CoordinatePair'
.\Coordinates.cpp(62) : warning C4280: 'operator ->' was self recursive through type 'CoordinatePair'
        y:\documents\wende-project\c3\wende_c3_executioncode\wende_c3_executioncode\c3_display\c3_app\Coordinates.h(7) : see declaration of 'CoordinatePair'
.\Coordinates.cpp(95) : error C2818: application of overloaded 'operator ->' is recursive through type 'CoordinatePair'
        y:\documents\wende-project\c3\wende_c3_executioncode\wende_c3_executioncode\c3_display\c3_app\Coordinates.h(7) : see declaration of 'CoordinatePair'
.\Coordinates.cpp(95) : warning C4280: 'operator ->' was self recursive through type 'CoordinatePair'
        y:\documents\wende-project\c3\wende_c3_executioncode\wende_c3_executioncode\c3_display\c3_app\Coordinates.h(7) : see declaration of 'CoordinatePair'
.\Coordinates.cpp(95) : error C2818: application of overloaded 'operator ->' is recursive through type 'CoordinatePair'
        y:\documents\wende-project\c3\wende_c3_executioncode\wende_c3_executioncode\c3_display\c3_app\Coordinates.h(7) : see declaration of 'CoordinatePair'
.\Coordinates.cpp(95) : warning C4280: 'operator ->' was self recursive through type 'CoordinatePair'
        y:\documents\wende-project\c3\wende_c3_executioncode\wende_c3_executioncode\c3_display\c3_app\Coordinates.h(7) : see declaration of 'CoordinatePair'

Looking up the error给了我以下定义:

  

重载'运算符 - >'的应用通过类型'type'递归:

     

类成员访问运算符的重新定义包含递归返回语句。重新定义 - >具有递归的运算符,必​​须将递归例程移动到从运算符覆盖函数调用的单独函数中。

我显然不知道我在这里做什么,需要设置正确的方向。帮助

1 个答案:

答案 0 :(得分:1)

帕特里克在评论中说,WorldBounds类型为CoordinatePairCoordinatePair^应为WorldBounds.x。正如您所提到的,该修复程序会导致其他编译器错误,您需要遍历当前执行的所有位置WorldBounds->x并将其替换为ref class

对于^,您几乎总是想要使用{{1}}。有些情况下你会想要把它关掉,但它们很少见。