试试使用 typename 工具:
template <class T>
class CSafeSet
{
public:
CSafeSet();
~CSafeSet();
typename std::set<T>::iterator Begin();
private:
std::set<T> _Set;
};
你需要在那里使用typename,因为它依赖于模板T。在代码上面的链接中有更多信息。如果使用typedef,许多这些东西会变得更容易:
template <class T>
class CSafeSet
{
public:
typedef T value_type;
typedef std::set<value_type> container_type;
typedef typename container_type::iterator iterator_type;
typedef typename container_type::const_iterator const_iterator_type;
CSafeSet();
~CSafeSet();
iterator_type Begin();
private:
container_type _Set;
};
顺便提一句,如果你想做到完整性,你需要允许CSafeSet像 set一样完成同样的事情,这意味着要使用自定义的比较器和分配器:
template <class T, class Compare = std::less<T>, class Allocator = std::allocator<T> >
class CSafeSet
{
public:
typedef T value_type;
typedef Compare compare_type;
typedef Allocator allocator_type;
typedef std::set<value_type, compare_type, allocator_type> container_type;
typedef typename container_type::iterator iterator_type;
typedef typename container_type::const_iterator const_iterator_type;
}
最后一条建议,如果你要创建一个类的包装器,请尽量遵循与该类相同的命名约定。也就是说,你的 Begin()
应该改为 begin()
(个人认为在类名前加 C 有点奇怪,但这取决于你 :]