2011-06-29 35 views
0

以下のコードの最後の2行で何が間違っていますか?私は、エラーメッセージが表示されます。リストの印刷方法<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