Note that there are some explanatory texts on larger screens.

plurals
  1. PODo I need to redefine the member selector operator in a User-Defined Type?
    text
    copied!<p>I'm likely doing something boneheaded here, and I'd like a pointer (pardon the pun).</p> <p>I've defined the following class:</p> <pre><code>ref class CoordinatePair { public: int x; int y; CoordinatePair(); CoordinatePair(int xInput, int yInput); CoordinatePair(CoordinatePair ^Other); //CoordinatePair&amp; operator-&gt;(); }; </code></pre> <p>Fairly simple. I find that I can select members using the <code>-&gt;</code> operator within the class's namespace with no ill effects. For example, the following compiles:</p> <pre><code>CoordinatePair::CoordinatePair(CoordinatePair ^Other) { x = Other-&gt;x; y = Other-&gt;y; } </code></pre> <p>Groovy. Yet, when I try to compile this, I get problems.</p> <pre><code>CoordinatePair^ Coordinates::TranslateCoords(CoordinatePair^ WorldCoords) { CoordinatePair^ newCoords = gcnew CoordinatePair(); float coordsRatio = 0.0; //Translate X coordsRatio = (float) WorldCoords-&gt;x / WorldBounds-&gt;x; newCoords-&gt;x = (int) (coordsRatio * PixelBounds-&gt;x); //Translate Y coordsRatio = 0.0; coordsRatio = (float) WorldCoords-&gt;y / WorldBounds-&gt;y; newCoords-&gt;y = (int) (coordsRatio * PixelBounds-&gt;y); return newCoords; } </code></pre> <p>(Note, in the above code, <code>WorldBounds</code> is a member of the <code>Coordinates</code> class. It itself is a <code>CoordinatePair</code> that defines the plane for my project.)</p> <p>Specifically I get this error:</p> <pre><code>.\Coordinates.cpp(95) : error C2819: type 'CoordinatePair' does not have an overloaded member 'operator -&gt;' </code></pre> <p>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:</p> <pre><code>CoordinatePair^ operator-&gt;(); </code></pre> <p>And I defined it like so:</p> <pre><code>CoordinatePair^ CoordinatePair::operator-&gt;() { return this; } </code></pre> <p>That made the compiler <strong>even angrier!</strong> :-(</p> <pre><code>.\Coordinates.cpp(17) : error C2818: application of overloaded 'operator -&gt;' 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 -&gt;' 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 -&gt;' 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 -&gt;' 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 -&gt;' 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 -&gt;' 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 -&gt;' 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 -&gt;' 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 -&gt;' 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 -&gt;' 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 -&gt;' 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 -&gt;' 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 -&gt;' 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 -&gt;' 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 -&gt;' 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 -&gt;' 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' </code></pre> <p><a href="http://msdn.microsoft.com/en-us/library/kty7akd3%28v=vs.80%29.aspx" rel="nofollow">Looking up the error</a> gave me the following definition:</p> <blockquote> <p><em>application of overloaded 'operator ->' is recursive through type 'type'</em></p> <p>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.</p> </blockquote> <p>I clearly don't know what I'm doing here and need to be set in the right direction. Help?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload