开发者

Comparing variables in two instances of a class

开发者 https://www.devze.com 2022-12-10 07:55 出处:网络
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 o

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.

now i was curious to know why the below doesnt work please:

(this bit of code compares two points: a & b, each with two points, a north-east (ne) and south-west (sw) and their x and y plots) if ((a->x_ne <= b->x_ne && a->y_ne <= b-> ne) && (a->x_sw => b->x_sw && a->y_sw => b-> sw)) { return true; } else return false;

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.

and also, what would be the CLEANEST and most elegant way to accomplish the above? have a friend function perhaps?

many thanks

edit: added some comments to (hopefully make the variables clearer)

// 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->x_ne <= b->x_ne && a->y_ne <= b-> ne) && 
//       (a->x_sw => b->x_sw && a->y_sw => b-> sw)) {
//     return true; 
//   } else return false;

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

// };


// 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)  { //c开发者_C百科onstructor
//     size = 2.3;
// }

//   void retreat() { //what happens in the case of loss in attack
//     /*
//     if(goldfish.size[1] - goldfish.size[2] <= 1 && goldfish.size[1] - goldfish.size[2] > 0) {
//       size = size - 0.2;
//     } 
//     */

//   }
//   void triumph() {
//   }

//   void attack() {

//   }
//   // void goldfish() 
// };


On first glance: There isn't a => operator. It's called >=


Assuming that your territories are rectangles and your are detecting overlap by comparing the corners of the two classes (ne and nw) you are only checking the northwest and northeast corners which have a region of a line. As @Éric Malenfant mentioned, you have structures as the class members which are accessed by the '.' operator. Those members are ne and sw so to reference them would be: "a.ne.x"

So starting with this:

if ((a->x_ne <= b->x_ne && a->y_ne <= b-> ne) && 
    (a->x_nw => b->x_nw && a->y_nw => b-> nw)) {
    return true; 
   } else return false;

Change it to:

return (   (a.ne.x <= b.ne.x && a.ne.y <= b.ne.y) 
        && (a.sw.x >= b.sw.x && a.sw.y >= b.sw.y));


What do you mean by "doesnt work"? I does not compile?

If contain_check is written as shown in your post, a problem is that you are using the arrow operator on non-pointers. Use dot instead:

    if ((a.x_ne <= b.x_ne && a.y_ne <= b.ne) //etc.


I noticed two possible problems right off (note: not a C++ expert):

You use => for "greater than or equal to", where it should be >=.

Also, I think b->ne should be b->y_ne.


bool contain_check(territory a, territory b)

You're passing in two territory objects, not pointers to territory objects. Consequently, you'll want to use the . operator to access members instead of the -> operator. Something like:

a.ne

Additionally, you've declared the ne and sw members private, which means that they won't be accessible to unrelated functions. They would need to be public for the contain_check() function to access them.


sorry, i was clearly (very) confused. thanks guys! below works:

if ((a.ne.x <= b.ne.x && a.ne.y <= b.ne.y) && 
      (a.sw.x >= b.sw.x && a.sw.y >= b.sw.y)) {
    return true; 
  } else return false;
  }


the method bool territory::contain_check(const territory &a, const territory &b); should be declared as static. it makes sense. or, better, write it as standalone function, because it has nothing to do with the class territory; it checks some kind of relation between two instances, right?

0

精彩评论

暂无评论...
验证码 换一张
取 消