简体   繁体   English

C++ 无法将对象添加到列表

[英]C++ Trouble adding objects to a list

I am having trouble adding objects to a list.我无法将对象添加到列表中。

I want to make a List of Persons, and I know the size of the list is fixed at 5 people.我想制作一个人员列表,我知道列表的大小固定为 5 人。 Each Person has an age (int) and a gender (string).每个人都有一个年龄(整数)和一个性别(字符串)。 I want to add the Persons to the List, but I don't know how, I've only worked with integers now that I think of it.我想将人员添加到列表中,但我不知道如何,我现在想到的只是使用整数。

Below is what I have so far.以下是我到目前为止所拥有的。 The random age and gender is working, however clunky that is.随机的年龄和性别是有效的,无论多么笨拙。 I'm thinking instead of creating the Persons as I did, maybe somehow I should create them dynamically in a for loop which will somehow generate the age and gender per loop iteration?我在想,而不是像我那样创建 Person ,也许我应该以某种方式在 for 循环中动态创建它们,这将以某种方式生成每次循环迭代的年龄和性别? Maybe the list should be pointers to the Person objects.也许列表应该是指向 Person 对象的指针。

#include <iostream>
#include <list>
#include <memory>

using namespace std;

class Person {
private:
    const int OLDEST_AGE = 100;
    const int YOUNGEST_AGE = 1;
    int age;
    string gender;
public:
    Person()
    {
        age = generateAge();
        gender = generateGender();
    }
    // Person: generate random age for Person, from 1-100
    int generateAge() {
        int randomAge;
        randomAge = rand() % (OLDEST_AGE - YOUNGEST_AGE + 1) + YOUNGEST_AGE;
        return randomAge;
    };
    // Person: generate random gender for Person, Male or Female
    string generateGender() {
        int genderNumber;
        genderNumber = rand() % (1 - 0 + 1) + 0;
        if (genderNumber == 1)
            return "Male";
        else
            return "Female";
    };
    void getStats() {
        cout << "Person Age: " << age << endl;
        cout << "Person Gender: " << gender << endl;
    };
};
int main()
{
    Person P1, P2, P3, P4, P5;
    // Just to see if the objects are created
    P1.getStats();
    P2.getStats();
    P3.getStats();
    P4.getStats();
    P5.getStats();

    list<Person> myList;
    list<Person>::iterator IT;

    for (int i = 1; i <= 5; i++)
        //don't know how to add each Person to the list
        myList.push_back(P1);

    cout << "myList contains: ";
    for (IT = myList.begin(); IT != myList.end(); IT++)
        // similar to above, how do I get the list to print each object's stats
        P1.getStats();
    cout << endl;

    return 0;
}

Using vector would be better in your case if you want to add a lot of elements one after the other as specified in another response to another question comparing list and vector.如果您想按照另一个比较列表和向量的问题的另一个回答中指定的那样一个接一个地添加大量元素,那么在您的情况下使用向量会更好。

Back to the question, as some comments specified, declaring a variable for each person is not a good practice.回到问题,正如一些评论指出的那样,为每个人声明一个变量并不是一个好习惯。 What you could do instead is to populate your list with a loop like so:你可以做的是用这样的循环填充你的列表:

for(int i = 0; i < 5; i++)
    myList.push_back(Person());

Then to access the objects you would just loop through the list again wherever you need to.然后要访问对象,您只需在需要的地方再次循环遍历列表即可。

for(int i = 0; i < 5; i++)
    myList[i].getStats();

You can create a vector, and run through a loop where you instantiate the person, and then push them into the vector.您可以创建一个向量,然后运行一个循环,在其中实例化此人,然后将它们推入向量中。 This way, you won't need to create separate person variables like p1, p2, etc., and your vector would hold all the "Person" objects.这样,您无需创建单独的人员变量,如 p1、p2 等,并且您的向量将保存所有“人员”对象。

vector<Person> people;
for (int i = 0; i < 5; i++) {
    people.push_back(Person());
}

for (Person person : people) {
    person.getStats();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM