저는 C++에 완전히 익숙하며 주어진 리터 퍼즐을 해결하는 프로그램을 작성하는 것이 좋습니다. (3 리터와 5 리터의 용량을 가진 2 개의 컨테이너가 있습니다. 4 리터를 얻는가?)함수는 C++에서 객체 속성을 변경하지 않습니다.
나는 주어진 컨테이너에 대한 클래스와 하나의 컨테이너의 내용물을 다른 것으로 '붓는'기능을 썼다. 그러나 클래스 전체가 공개 임에도 불구하고이 함수는 모든 객체의 내용 값을 변경하지 않습니다. 내가 뭘 잘못하고 있는지 모르겠다.
#include <iostream>
using namespace std;
class Container {
public:
int quantity; //quantity of water in container
int size; //max amt of water
};
void pour(Container a, Container b) {
int differential = b.size - b.quantity;
if (a.quantity <= differential) {
b.quantity = a.quantity + b.quantity;
a.quantity = 0;
}
else if (a.quantity > differential) {
b.quantity = b.quantity - differential;
a.quantity = a.quantity - differential;
}
};
int main() {
Container bottle1;
bottle1.quantity = 5;
bottle1.size = 6;
Container bottle2;
bottle2.quantity = 0;
bottle2.size = 2;
pour(bottle2, bottle1);
cout << bottle1.quantity << ", " << bottle2.quantity << endl;
return 0;
}
내 실수가 분명하다 확신하지만 난 아무데도 답변을 찾을 수 없습니다
여기 내 코드입니다. 어떤 도움이라도 대단히 감사하겠습니다.
인수 대신 값으로 참조하십시오. 'void pour (Container & a, Container & b)' – Jarod42