我有一个关于我正在构建的代码的快速问题。基本上我想比较一个类(金鱼)的两个实例之间的变量,看看一个是否在另一个的范围内。它们都有领土分类,而领土分类又使用由 x 和 y 数据点组成的点分类。
现在我很想知道为什么下面的方法不起作用:
(这段代码比较两个点:a 和 b,每个点有两个点,东北 (ne) 和西南 (sw) 以及它们的 x 和 y 图) 如果((a->x_ne <= b->x_ne && a->y_ne <= b-> ne)&& (a->x_sw => b->x_sw && a->y_sw => b-> sw)) { 返回真; } 否则返回假;
我可以想出一个解决方法(例如,通过获取位置方法),并在主体中使用一个函数进行比较,但我很想知道——作为一个崭露头角的 c++ 程序员——为什么会出现上述情况,或类似的实现似乎不起作用。
此外,完成上述任务的最简洁、最优雅的方法是什么?也许有 friend 功能?
非常感谢
编辑:添加了一些注释(希望能使变量更清晰)
// 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) { //constructor
// 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()
// };
最佳答案
乍一看:没有 => 运算符。这叫做 >=
关于c++ - 比较类的两个实例中的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1669390/