如何打印列表<类*> LS?

how to print a list<class*> ls?

本文关键字:gt LS 何打印 lt 打印 列表      更新时间:2023-10-16

我在下面代码的最后两行做错了什么?我收到一个错误:

‘t.std::_List_iterator<_Tp>::operator* [with _Tp = a*]()’中的成员‘name’的请求,其为非类类型‘a*’

#include <dlfcn.h>
#include <iostream>
#include "/home/developer/Desktop/MsgSphSDK1/test1_sdk/libsdk_MS.hpp"
#include <list>
#include <typeinfo>
#include <string>
using namespace std;
class a
{
public:
    string name;
    int age;
};
int main()
{
    a* l = new a();
    l->name = "me";
    l->age = 1;
    list<a*> ls;
    list<a*>::iterator t;
    for (t = ls.begin(); t != ls.end(); ++t) 
        cout << (*t).name << endl;
}

您应该编写

cout<<(*t)->name<<endl

t是一个迭代器,(*t)为您提供a*(指向类a的对象的指针)。因此,要访问对象的成员,应该使用->,而不是.