-4
#include <iostream>

class Person { //creating a Person class with attributes like ID, latitude (X) and longitude (y)
public:
    Person(int _id, int _x, int _y, bool _infected) : id(_id), x(_x), y(_y), infected(_infected) {}

    int id;
    int x;
    int y;
    bool infected;
};

void display(const Person people[], int numPeople)
 { // Display movements and infection status
    system("cls");  // Clear console

    for (int i = 0; i < numPeople; ++i) {
        std::cout << "Person " << people[i].id << ": ";
        std::cout << "Location (" << people[i].x << ", " << people[i].y << ")";
        std::cout << " - Infected: " << (people[i].infected ? "Yes" : "No") << std::endl;
    }
}

int main() {
    srand(time(nullptr));

    const int numPeople = 100; // creating 100 people with random locations and infections
    const int numInfected = numPeople * 0.1;
    const int n = 7;  // Number of days for simulation

    Person people[numPeople];

    for (int i = 0; i < numPeople; ++i) {
        int x = rand() % 201 - 100;  // [-100, 100]
        int y = rand() % 201 - 100;
        bool infected = i < numInfected;

        people[i] = Person(i + 1, x, y, infected);
    }

    for (int day = 1; day <= n; ++day) { // simulation movement for n days
        for (int i = 0; i < numPeople; ++i) {
            if (!people[i].infected) {
                int dx = rand() % 3 - 1;  // -1, 0, 1
                int dy = rand() % 3 - 1;

                people[i].x += dx;
                people[i].y += dy;
            }
        }

        for (int i = 0; i < numPeople; ++i) {  // Tracking infections
            if (people[i].infected) {
                for (int j = 0; j < numPeople; ++j) {
                    if (!people[j].infected) {
                        double distance = sqrt(pow(people[i].x - people[j].x, 2) + pow(people[i].y - people[j].y, 2));
                        if (distance <= sqrt(2)) {
                            if (rand() % 100 < 90) {
                                people[j].infected = true;
                            }
                        }
                    }
                }
            }
        }
        display(people, numPeople);
    }

    int numInfectedAtEnd = 0; // Count infected people

    for (int i = 0; i < numPeople; ++i) {

        if (people[i].infected) {
            numInfectedAtEnd++;
        }
    }

    std::cout << "Number of infected people at the end: " << numInfectedAtEnd << std::endl;

    return 0;
}

I tried the call but it seems that it still couldnt run so please anyone can help out

prapin
  • 6,395
  • 5
  • 26
  • 44
irazff
  • 1
  • 3
    C++ and C# are two ***very*** different languages. Don't add tags that aren't relevant to your question. Also please read [the help pages](http://stackoverflow.com/help), take the SO [tour], and read [ask]. Then read [how to write the "perfect" question](https://codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question/), especially its [checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). And lastly **[edit]** your question to improve it. – Some programmer dude Aug 28 '23 at 10:33
  • You need to define default constructor for `Person` class. – Aamir Aug 28 '23 at 10:39
  • 2
    Creating an array of `people` will try to initialize them all, but the `Person` constructor requires 4 parameters. Perhaps try a `std::vector` and then `push_back` each `Person` when constructed. – BoP Aug 28 '23 at 10:39
  • 2
    C++ is case sensitive and `PERSON::PERSON()` is not the same as `Person::Person()`. Moreover writing in all caps is considered as shouting and is not nice – 463035818_is_not_an_ai Aug 28 '23 at 10:44
  • The definition `Person people[numPeople];` will create an array of `numPeople` elements, each element is a *default* constructed `Person` object. You don't have a `Person` default constructor, so the compiler complains. One way to solve that problem is to create a `Person` default constructor. Another is to use a `std::vector` and add to it when needed. – Some programmer dude Aug 28 '23 at 10:53
  • Does this answer your question? [no default constructor exists for class](https://stackoverflow.com/questions/4981241/no-default-constructor-exists-for-class) – pptaszni Aug 28 '23 at 11:19

1 Answers1

1

As @Bop wrote in the comments, you try to create an array of objects, that need a default constructor:

Person people[numPeople];

As you create them a few lines later anyway, I suggest turning the array into a vector and extracting the whole creation process into a function:

[[nodiscard]] std::vector<Person> CreatePersons()
{
   std::vector<Person> persons;

   for (int i = 0; i < numPeople; ++i) {
        int x = rand() % 201 - 100;  // [-100, 100]
        int y = rand() % 201 - 100;
        bool infected = i < numInfected;

        // Create a Person object and add it via emplace_back
        persons.emplace_back(i + 1, x, y, infected);
   }

   return persons;
}

You can assign it like this in your main():

auto persons = CreatePersons()

This way, you don't need the default constructor (if you don't want one/can't have one).

For this to work, you also should move the magic numbers to a more global scope, to make it more clear, that they are used in other places as well

    constexpr int numPeople = 100; // creating 100 people with random locations and infections
    constexpr int numInfected = numPeople * 0.1;
    constexpr int n = 7;  // Number of days for simulation
SAlex
  • 361
  • 2
  • 10