0

Создал класс для хранения пользовательского типа данных. Есть какая-то возможность теперь просмотреть этот контейнер?

#include <iostream>
#include <list>
#include <algorithm>
using namespace std;
class Test {
private:
    string name;
    int age;
public:
    Test(string namee, int agee) {
        name = namee;
        age = agee;
    }
};

void main()
{
    setlocale(LC_ALL, "");
    list <Test> q;
    list <Test> qq;
    srand(time(NULL));
        q.push_front(Test("Dima", 2));
        q.push_front(Test("Katya", 60));

}
1
  • 2
    int main()! Не void!
    – Harry
    29 апр 2021 в 18:51

1 ответ 1

3

Например, так:

class Test {
private:
    string name;
    int age;
public:
    Test(string namee, int agee) {
        name = namee;
        age = agee;
    }
    string get_name() const { return name; }
    int    get_age()  const { return age;  }

};

int main()
{
    setlocale(LC_ALL, "");
    list <Test> q;
    list <Test> qq;
    srand(time(NULL));
    q.push_front(Test("Dima", 2));
    q.push_front(Test("Katya", 60));

    for(auto t: q)
    {
        cout << t.get_name() << " " << t.get_age() << endl;
    }

}

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.