制作类和类指针列表时出错

时间:2013-05-02 03:57:17

标签: c++

我正在尝试创建如下列表。但是有错误。

#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’

请有人知道这件事,请帮助我。

2 个答案:

答案 0 :(得分:2)

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

忽略分配器,std::list只采用一种类型,而不是两种类型。

allocator必须是满足STL分配器要求的类,它不能是指针。

答案 1 :(得分:1)

您可能需要地图,而不是列表。如果你想要一个包含这两个类的列表,你可以使用std :: pair来使这两个类看起来像一个单独的元素:

 typedef std::list<std::pair<class2, class3*> > m_list;