开发者

how to print a list<class*> ls?

What am I doing wrong at the last two lines in my code below? I receive an error:

request for member ‘name’ in ‘t.std::_List_iterator<_Tp>::operator* [with _Tp = a*]()’, which is of non-class type ‘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;
}


You should write

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

t is an iterator, (*t) gives you a* (the pointer to the object of class a). So to get access to the member of the object you should use ->, not ..

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜