我很可能在這裏做了些什麼,我想要一個指針(原諒雙關語)。我是否需要重新定義用戶定義類型中的成員選擇器運算符?
我已經定義了以下類:
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;
}
(注意,在上面的代碼,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給我的定義如下:
application of overloaded 'operator ->' is recursive through type 'type'
A redefinition of the class member access operator contains a recursive return statement. To redefine the -> operator with recursion, you must move the recursive routine to a separate function called from the operator override function.
我顯然不知道我在做什麼,在這裏,需要在正確的方向設置幫助
?
當您有實例或對實例的引用(而不是指針)時,通常會給出此錯誤。所以你應該寫instance.x而不是instance-> x。你的代碼中的WorldsBounds的定義是什麼? – Patrick
編譯器也會出錯。它抱怨說類型是'CoordinatePair ^',並詢問我是否打算使用' - >'。我更新了關於「WorldBounds」是什麼的上下文。 –