0

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

我定义了以下类:

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

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

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'

查找错误给了我以下定义:

重载 'operator ->' 的应用是通过类型 'type' 递归的

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

我显然不知道我在这里做什么,需要朝着正确的方向前进。帮助?

4

1 回答 1

1

正如帕特里克在评论中所说,当它应该WorldBounds是. 正如您所提到的,该修复会导致其他编译器错误,您需要遍历您当前所做的所有地方并将其替换为.CoordinatePairCoordinatePair^WorldBounds.xWorldBounds->x

对于 a ref class,您几乎总是想使用^. 在某些情况下,您会想要将其关闭,但这种情况很少见。

于 2012-06-06T17:51:26.963 回答