읽은 후 특정 정보를 처리하고 시간별로 구조체 벡터를 정렬하는 다음 프로그램을 작성하지만 작동하지 않고 오류 메시지가 표시됩니다.
`no matching function for call to ‘swap(collisionOfCars&, collisionOfCars&)’`,`error: no type named ‘type’ in ‘struct std::enable_if<false, void>’` and `error: use of deleted function ‘collisionOfCars& collisionOfCars::operator=(collisionOfCars&&)’ *(__first + __holeIndex) = _GLIBCXX_MOVE(*(__first + __secondChild));`
내 생각 엔 정렬 기능에 몇 가지 문제가 있지만 해결 방법을 모르겠습니다. 다음은 내 코드입니다.
class Car{
public:
string ID;
double xPos, yPos, xVel, yVel;
bool collided;
Car(){
}
Car(string ID, double xPos, double yPos, double xVel, double yVel, bool collided){
this->ID = ID;
this->xPos = xPos;
this->yPos = yPos;
this->xVel = xVel;
this->yVel = yVel;
this->collided = collided;
}
};
struct collisionOfCars{
public:
Car &firstCar;
Car &secondCar;
double time;
};
bool tupleCompare(collisionOfCars first, collisionOfCars second) {
return first.time < second.time;
}
collisionOfCars willCollide(Car &car1, Car &car2) {
double posDifX = car1.xPos - car2.xPos;
double posDifY = car1.yPos - car2.yPos;
double velDifX = car1.xVel - car2.xVel;
double velDifY = car1.yVel - car2.yVel;
double a = velDifX*velDifX + velDifY*velDifY;
double b = posDifX*velDifX+posDifY*velDifY;
double c = posDifX*posDifX + posDifY*posDifY - 100;
double possibleSol = b*b - a*c; // b^2-4ac
double sol1, sol2;
if ((a == 0) || possibleSol < 0) { // check for a = 0 or b^2-4ac < 0
return collisionOfCars{car1, car2, -1};
} else if (possibleSol == 0) {
sol1 = -b/(2*a);
if(sol1 >= 0){
return collisionOfCars{car1, car2, sol1};
}
} else {
sol1 = (-b + sqrt(possibleSol))/(a);
sol2 = (-b - sqrt(possibleSol))/(a);
if (sol1 <= sol2 && sol1 >= 0) {
return collisionOfCars{car1, car2, sol1};
} else if (sol2 <= sol1 && sol2 >= 0) {
return collisionOfCars{car1, car2, sol2};
} else {
return collisionOfCars{car1, car2, -1};
}
}
}
int main() {
vector< Car > collection; // All info of cars
vector< collisionOfCars > collision; // all possible collision
string ID;
double xPos, yPos, xVel, yVel;
while (cin >> ID >> xPos >> yPos >> xVel >> yVel) {
collection.push_back(Car(ID, xPos, yPos, xVel, yVel, false));
}
for (int i = 0; i < collection.size(); i++) {
for (int j = i+1; j < collection.size(); j++) {
collisionOfCars result = willCollide(collection[i], collection[j]);
if (result.time != -1) {
collision.push_back(result);
}
}
}
sort(collision.begin(), collision.end(), tupleCompare); // where goes wrong
}
궁금한 점이 있으시면 구조체의 참조를 사용하여 구조체로 전달되는 Car 변경 사항이 vector <Car> 컬렉션에 반영되는지 확인합니다.
포인터에 대한 참조를 변경했지만 이제 다음과 같은 새로운 오류 메시지가 표시됩니다.
cannot convert ‘Car’ to ‘Car*’ in initialization
return collisionOfCars{car1, car2, sol1};
위해서는 std::sort()
일에, std::swap()
객체의 유형에 대해 인스턴스화해야 정렬되는 ( collisionOfCars
). 이를 위해서는 복사 또는 이동 할당이 가능해야합니다 (또는 std::swap()
전문화 가 있어야합니다 ). 유형이 두 연산자를 선언하지 않고 컴파일러가 해당 연산자를 생성하기위한 요구 사항을 충족하지 않기 때문에 생성하지 않습니다.
이는 유형에 참조 유형의 비 정적 데이터 멤버가 있기 때문입니다. (참조 는 재 장착 할 수 없으므로 서로 다른 개체를 참조하도록 두 참조를 바꿀 수 없습니다. 참조 된 개체의 내용 만 바꿀 수 있습니다. 이는 원하는 것이 아닙니다.)
클래스 T에 대한 기본 복사 할당 연산자 는 다음 중 하나라도 참인 경우 삭제 된 것으로 정의됩니다 .
...
- T에는 참조 유형의 비 정적 데이터 멤버가 있습니다.
다음 중 하나라도 참인 경우 클래스 T에 대한 암시 적으로 선언되거나 기본값이 지정된 이동 할당 연산자는 삭제 된 것으로 정의됩니다 .
...
- T에는 참조 유형의 비 정적 데이터 멤버가 있습니다.
것을이 수단 collisionOfCars
인스턴스가 서로의 상단에 할당 할 수 없습니다 이가되어 필요한 벡터를 정렬합니다. (다른 요소를 서로 교환하지 않으면 벡터를 정렬 할 수 있습니까?)
가장 간단한 해결책은 (reference-to- ) 대신 (pointer-to- ) collisionOfCars
를 사용 하도록 변경 하는 것입니다 . (어쨌든 포인터 주위의 래퍼 일뿐입니다) 사용할 수도 있습니다 .Car *
Car
Car &
Car
std::reference_wrapper<Car>
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다