C++ 为什么我在传染病模拟中实现旅行时什么都没有发生?
我的传染病模拟有问题。以下是相关代码:C++ 为什么我在传染病模拟中实现旅行时什么都没有发生?,c++,sfml,C++,Sfml,我的传染病模拟有问题。以下是相关代码: void City::updateTravel(City &cityDestination) { vector<bool> travelArray; for (int i = 0; i < population; i++) { if (people[i].travel(cityDestination.getCity())) { travelArray.push_back
void City::updateTravel(City &cityDestination) {
vector<bool> travelArray;
for (int i = 0; i < population; i++) {
if (people[i].travel(cityDestination.getCity())) {
travelArray.push_back(true);
}
else {
travelArray.push_back(false);
}
}
vector<Person> newArray;
for (int i = 0; i < travelArray.size(); i++) {
if (travelArray[i] == true) {
cityDestination.add(people[i]);
cityDestination.setPopulation(cityDestination.getPeople().size());
}
else {
newArray.push_back(people[i]);
}
}
people = newArray;
this->setPopulation(people.size());
}
这是城市班:
class City
{
private:
std::vector<Person> people;
float populationDensity;
int population;
int activeCases;
int totalCases;
int cellSize;
Vector2i citySize;
RectangleShape city;
public:
City(Vector2f position, int startingInfection);
~City();
void updateInfection();
void updateTravel(City &cityDestination);
int getActiveCases();
int getTotalCases();
int getPopulation();
std::vector<Person> &getPeople();
void add(Person newPeople);
RectangleShape getCity();
int getCellSize();
void setPosition(Vector2f position);
void setPopulation(int newPopulation);
};
在main.cpp中,我只有以下内容:(遍历所有城市对)
for(int i=0;i<2;i++){
对于(int j=0;j<2;j++){
如果(i!=j){
城市[i].updateTravel(城市[j]);
}
}
}
问题是updateTravel函数什么都不做。如果我把它说出来,感染的数量保持不变,这显然是不可能的。我似乎已经尝试了一切,包括删除updateTravel中的布尔数组,处理Person类中的所有旅行,以及直接在City类中旅行,而main.cpp中没有代码。我不知道它为什么不工作。如何定义这些循环的计数器似乎是个问题:
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
if (i != j) {
cities[i].updateTravel(cities[j]);
}
}
}
for(int i=0;i<2;i++){
对于(int j=0;j<2;j++){
如果(i!=j){
城市[i].updateTravel(城市[j]);
}
}
}
试试这个:
for (int i = 0; i < 2; i++) {
for (int j = i+1; j < 2; j++) {
if (i != j) {
cities[i].updateTravel(cities[j]);
}
}
}
for(int i=0;i<2;i++){
对于(int j=i+1;j<2;j++){
如果(i!=j){
城市[i].updateTravel(城市[j]);
}
}
}
它解决了问题吗?我们需要“帮助修复bug”类型的问题,以包括一个特定的问题,因为MRE是强大调试技术的结晶。构建一个好的MRE,你通常会自己发现并解决问题,而不需要问这个问题。现在问题已经解决了,但我下次会记住这一点。
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
if (i != j) {
cities[i].updateTravel(cities[j]);
}
}
}
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
if (i != j) {
cities[i].updateTravel(cities[j]);
}
}
}
for (int i = 0; i < 2; i++) {
for (int j = i+1; j < 2; j++) {
if (i != j) {
cities[i].updateTravel(cities[j]);
}
}
}