2011-06-29 55 views
0

我在下面的代码中最后两行发生了什么错误?我收到一个错误:如何打印清单<class*> ls?

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; 
} 
+0

为什么'main()'没有返回什么东西? – Donotalo

+1

@Donotalo:因为它不需要。 – Xeo

+0

@Xeo:是的,它的确如此。但你不必告诉它。 :) –

回答

6

你应该写

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

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

+0

thx。有用!原汁原味 – marryy