Note that there are some explanatory texts on larger screens.

plurals
  1. POComparing variables in two instances of a class
    text
    copied!<p>i have what i hope is a quick question about some code i am building out.. basically i want to compare the variables amongst two instances of a class (goldfish) to see if one is inside the territory of another. they both have territory clases which in turn use a point clase made up of an x and y data-point.</p> <p>now i was curious to know why the below doesnt work please:</p> <p>(this bit of code compares two points: a &amp; b, each with two points, a north-east (ne) and south-west (sw) and their x and y plots) if ((a->x_ne &lt;= b->x_ne &amp;&amp; a->y_ne &lt;= b-> ne) &amp;&amp; (a->x_sw => b->x_sw &amp;&amp; a->y_sw => b-> sw)) { return true; } else return false;</p> <p>I can think of a work around (for instance, by having a get location method), and using a function in the main body to compare, but im curious to know --as a budding c++ programmer -- why the above, or a similar implementation doesnt appear to work.</p> <p>and also, what would be the CLEANEST and most elegant way to accomplish the above? have a friend function perhaps?</p> <p>many thanks </p> <p>edit: added some comments to (hopefully make the variables clearer)</p> <pre><code>// class point { // public: // float x; // float y; // point(float x_in, float y_in) { //the 2 arg constructor // x = x_in; // y = y_in; // } // }; // class territory { // private: // point ne, sw; // public: // territory(float x_ne, float y_ne, float x_sw, float y_sw) // : ne(x_ne, y_ne), sw(x_sw,y_sw) { // } // bool contain_check(territory a, territory b) { // //checks if a is contained in b (in THAT order!) // if ((a-&gt;x_ne &lt;= b-&gt;x_ne &amp;&amp; a-&gt;y_ne &lt;= b-&gt; ne) &amp;&amp; // (a-&gt;x_sw =&gt; b-&gt;x_sw &amp;&amp; a-&gt;y_sw =&gt; b-&gt; sw)) { // return true; // } else return false; </code></pre> <p>^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^</p> <pre><code>// }; // class goldfish { // protected: // float size; // point pos; // territory terr; // public: // goldfish(float x, float y) : pos(x,y), terr(x-1,y-1,x+1,y+1) { //constructor // size = 2.3; // } // void retreat() { //what happens in the case of loss in attack // /* // if(goldfish.size[1] - goldfish.size[2] &lt;= 1 &amp;&amp; goldfish.size[1] - goldfish.size[2] &gt; 0) { // size = size - 0.2; // } // */ // } // void triumph() { // } // void attack() { // } // // void goldfish() // }; </code></pre>
 

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