2013-05-02 33 views
-2

我想創建一個如下所示的列表。但有錯誤。錯誤,同時製作一個類和類指針的列表

#ifndef CLASS1_H 
#define CLASS1_H 
#include <list> 
class class2 
{ 

}; 
class class3 
{ 

}; 
class class1 
{ 
public: 
    typedef std::list<class2, class3*> m_list; 
    m_list mylist; 
}; 
#endif 

的錯誤是:

In file included from /usr/lib/gcc/i686-pc-cygwin/4.5.3/include/c++/list:63:0, 
       from class1.h:3: 
/usr/lib/gcc/i686-pc-cygwin/4.5.3/include/c++/bits/stl_list.h: In instantiation of ‘std::_List_base<class2, class3*>’: 
/usr/lib/gcc/i686-pc-cygwin/4.5.3/include/c++/bits/stl_list.h:418:5: instantiated from ‘std::list<class2, class3*>’ 
class1.h:16:9: instantiated from here 
/usr/lib/gcc/i686-pc-cygwin/4.5.3/include/c++/bits/stl_list.h:294:9: error: ‘class3*’ is not a class, struct, or union type 
/usr/lib/gcc/i686-pc-cygwin/4.5.3/include/c++/bits/stl_list.h:296:60: error: ‘class3*’ is not a class, struct, or union type 
/usr/lib/gcc/i686-pc-cygwin/4.5.3/include/c++/bits/stl_list.h: In instantiation of ‘std::list<class2, class3*>’: 
class1.h:16:9: instantiated from here 
/usr/lib/gcc/i686-pc-cygwin/4.5.3/include/c++/bits/stl_list.h:420:58: error: ‘class3*’ is not a class, struct, or union type 
/usr/lib/gcc/i686-pc-cygwin/4.5.3/include/c++/bits/stl_list.h:449:20: error: no members matching ‘std::list<class2, class3*>::_Base::_M_get_Tp_allocator’ in ‘class std::list<class2, class3*>::_Base’ 
/usr/lib/gcc/i686-pc-cygwin/4.5.3/include/c++/bits/stl_list.h:450:20: error: no members matching ‘std::list<class2, class3*>::_Base::_M_get_Node_allocator’ in ‘class std::list<class2, class3*>::_Base’ 

請任何人知道對此,請幫助我。

+0

你讀過列表文檔嗎? – Chethan 2013-05-02 04:02:07

+0

是的,Chethan,我現在讀它...謝謝:) – suma 2013-05-02 04:07:31

回答

2
template< 
    class T, 
    class Allocator = std::allocator<T> 
> class list; 

忽略分配器,std::list只取一個類型,而不是兩個。

分配器必須是滿足STL分配器要求的類,它不能是指針。

1

您可能需要地圖而不是列表。如果你想要這兩個類的列表,你可以使用std :: pair使這兩個類顯示爲一個單獨的元素:

typedef std::list<std::pair<class2, class3*> > m_list; 
相關問題