두 개체를 비교하려고하는데 오류가 계속 발생합니다.
바이너리 '==': 'robot'은이 연산자를 정의하지 않거나 사전 정의 된 연산자가 허용하는 유형으로의 변환을 정의하지 않습니다.
이것은 내 cpp 파일입니다.
void move(const robot r, vector<robot>& vec_r) {
// 0 = north, 1 = east, 2 = south, 3 = west
int x = r.xpos();
int y = r.ypos();
// move the robot depending on which direction it's facing
if (direction == 0) { ++y; }
else if (direction == 1) { ++x; }
else if (direction == 2) { --y; }
else if (direction == 3) { --x; }
// check if space is occupied and also if in enemy team, delete the robot
for (auto &p : vec_r) {
if (x == p.xpos() && y == p.ypos() /* && r.teamNo() == p.teamNo() */ ) {
find(vec_r.begin(), vec_r.end(), [&]() {
return robot(r.id(), r.teamNo(), x, y) == robot(p.id(), p.teamNo(), p.xpos(), p.ypos());
});
}
}
cout << r.id() << ' ' << r.teamNo() << ' ' << x << ' ' << y << ' ' << "\n";
}
이것은 내 헤더 파일입니다.
#ifndef ROBOT_H
#define ROBOT_H
#include <vector>
#include <string>
class robot {
int _id;
int _teamNo;
int _xpos;
int _ypos;
public:
robot(const int &id, int teamNo, int xpos, int ypos) :
_id(id), _teamNo(teamNo), _xpos(xpos), _ypos(ypos) {}
// Accessor functions for robot details
int id() const { return _id; }
int teamNo() const { return _teamNo; }
int xpos() const { return _xpos; }
int ypos() const { return _ypos; }
int getDirection() { return direction; };
int Compare(const robot& r) const;
bool operator == (const robot& r) const {
return 0 == Compare(r);
}
private:
int direction = 0;
};
#endif
자신의 클래스 유형에 대해 연산자를 정의 할 수 operator==
있습니다.
class robot {
// ... all your other stuff
public:
bool operator==(const robot& other) const {
return _id == other._id; // Use whatever logic makes sense for you here
}
};
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다