I’m likely doing something boneheaded here, and I’d like a pointer (pardon the pun).
I’ve defined the following class:
ref class CoordinatePair {
public:
int x;
int y;
CoordinatePair();
CoordinatePair(int xInput, int yInput);
CoordinatePair(CoordinatePair ^Other);
//CoordinatePair& operator->();
};
Fairly simple. I find that I can select members using the -> operator within the class’s namespace with no ill effects. For example, the following compiles:
CoordinatePair::CoordinatePair(CoordinatePair ^Other) {
x = Other->x;
y = Other->y;
}
Groovy. Yet, when I try to compile this, I get problems.
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;
}
(Note, in the above code, WorldBounds is a member of the Coordinates class. It itself is a CoordinatePair that defines the plane for my project.)
Specifically I get this error:
.\Coordinates.cpp(95) : error C2819: type 'CoordinatePair' does not have an overloaded member 'operator ->'
Huh. Well, okay. My attempt to research this problem drove me to try overloading the operator. So, I added the following in the class declaration:
CoordinatePair^ operator->();
And I defined it like so:
CoordinatePair^ CoordinatePair::operator->() {
return this;
}
That made the compiler even angrier! 🙁
.\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 gave me the following definition:
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.
I clearly don’t know what I’m doing here and need to be set in the right direction. Help?
As Patrick said in the comments, you’ve got
WorldBoundsas typeCoordinatePairwhen it should beCoordinatePair^. As you mentioned, that fix causes other compiler errors, you’ll need to go through all the places you currently doWorldBounds.xand replace it withWorldBounds->x.For a
ref class, you’ll almost always want to use the^. There are situations where you’ll want to leave it off, but they’re rare.