error returning std::set::iterator in template

I'm making a template wrapper around std::set. Why do I get error for Begin() function declaration?

template <class T>
class CSafeSet
{
    public:
        CSafeSet();
        ~CSafeSet();

        std::set<T>::iterator Begin();

    private:
        std::set<T> _Set;
};

error: type ‘std::set, std::allocator<_CharT> >’ is not derived from type ‘CSafeSet’

13.10.2009 17:35:42
As a side note: Please read this about using underscore in identifiers. stackoverflow.com/questions/228783/…
Martin York 13.10.2009 18:39:49
Short version: "Don't start identifier names with underscores"
jalf 13.10.2009 19:03:46
Long version: Underscores followed by a capital letter are reserved for the compiler. Double underscores followed by anything are reserved for the compiler as well.
GManNickG 13.10.2009 22:03:23
RiaD 27.12.2015 01:00:09
1 ОТВЕТ
РЕШЕНИЕ

Try typename:

template <class T>
class CSafeSet
{
    public:
        CSafeSet();
        ~CSafeSet();

        typename std::set<T>::iterator Begin();

    private:
        std::set<T> _Set;
};

You need typename there because it is dependent on the template T. More information in the link above the code. Lot's of this stuff is made easier if you use typedef's:

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

On a side note, if you want to be complete you need to allow CSafeSet to do the same thing as a set could, which means using a custom comparer and allocator:

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;

        // ...
}

And a last bit of advice, if you are going to create a wrapper around a class, try to follow the same naming conventions as where the class came from. That is, your Begin() should probably be begin() (And personally I think C before a class name is strange but that one is up to you :])

17
13.10.2009 22:02:38
Notice there's an error on your CSafeSet class template. You also need typename to define the iterators types because they are also dependent. Example: typedef typename container_type::iterator iterator_type; Without that typename it shouldn't compile in a standard conformant compiler. I could guess that you tried it in Visual Studio (version before 2008, when I think that was fixed), which erroneoulsy accepted such lines without typename.
Leandro T. C. Melo 13.10.2009 18:49:47
I didn't try at all, actually. Thanks for pointing that out, I was being silly (very common) :)
GManNickG 13.10.2009 22:02:17
BTW, I thought using upper-case names will emphasize it's not an original STL class. I am not sure I am going to implement all std::set methods in my CSafeSet.
jackhab 14.10.2009 08:14:30