c++ - 比较类的两个实例中的变量

标签 c++ oop

我有一个关于我正在构建的代码的快速问题。基本上我想比较一个类(金鱼)的两个实例之间的变量,看看一个是否在另一个的范围内。它们都有领土分类,而领土分类又使用由 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/

相关文章:

c++ - 为 iOS 创建 Qt 5.1 应用程序

javascript - 从 js 类分配事件监听器

oop - MATLAB - 设置/获取结构字段的访问权限?

php - OO数据库类

c++ - 回溯函数

c++ - sockaddr_in 获取错误的 IP 地址,cpp

python - 尝试在 C++ 中重现 PIL 调整大小图像的输出

c++ - 使用 g++ 编译时如何禁用候选信息?

c++ - 在 C++ 中使用虚方法的段错误

java - 使用单一数据结构的双向映射