【问题标题】:Do I need to redefine the member selector operator in a User-Defined Type?我是否需要在用户定义类型中重新定义成员选择器运算符?
【发布时间】:2012-06-06 06:58:07
【问题描述】:

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

我定义了以下类:

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'

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

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

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

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

【问题讨论】:

  • 当您有实例或对实例的引用(而不是指针)时,通常会出现此错误。所以你应该写 instance.x 而不是 instance->x。您的代码中对 WorldsBounds 的定义是什么?
  • 编译器也会出错。它抱怨类型是CoordinatePair ^,并询问我是否打算使用->。我用WorldBounds 是什么的上下文更新了我的问题。

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


【解决方案1】:

正如帕特里克在 cmets 中所说,你有 WorldBounds 作为类型 CoordinatePair,而它应该是 CoordinatePair^。正如您所提到的,该修复会导致其他编译器错误,您需要遍历您当前执行的所有位置 WorldBounds.x 并将其替换为 WorldBounds->x

对于ref class,您几乎总是希望使用^。在某些情况下,您可能希望将其关闭,但这种情况很少见。

【讨论】:

  • 那是我的密集。 >.
  • 原来我真的是那么笨。现在我想起来完全有道理,当我不处理参考时,-> 不会进行成员选择。谢谢。
猜你喜欢
  • 2020-09-12
  • 1970-01-01
  • 2017-06-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多