【发布时间】: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;
}
(注意,在上面的代码中,WorldBounds 是 Coordinates 类的成员。它本身就是为我的项目定义平面的 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