2012-06-06 58 views
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; 
} 

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給我的定義如下:

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.

我顯然不知道我在做什麼,在這裏,需要在正確的方向設置幫助

+1

當您有實例或對實例的引用(而不是指針)時,通常會給出此錯誤。所以你應該寫instance.x而不是instance-> x。你的代碼中的WorldsBounds的定義是什麼? – Patrick

+0

編譯器也會出錯。它抱怨說類型是'CoordinatePair ^',並詢問我是否打算使用' - >'。我更新了關於「WorldBounds」是什麼的上下文。 –

回答

1

正如帕特里克在評論中所說,你有WorldBounds作爲CoordinatePair類型,當它應該是CoordinatePair^正如你所說,這個修復會導致其他編譯器錯誤,你需要通過所有你curre的地方請執行WorldBounds.x並將其替換爲WorldBounds->x

對於ref class,您幾乎總是想使用^。有些情況下你會想離開它,但它們很少見。

+0

那個密密麻麻的我。當我回家時我會試試這個。 –

+0

原來我真的是那個笨蛋。現在我認爲它是完全意義上的,當我不處理參考時,' - >'不會做成員選擇。謝謝。 –

相關問題