derive from std::set, const_iterator does not work

Discussion in 'C++' started by Markus Dehmann, Jun 8, 2008.

  1. I want to derive from std::set, like shown below. But when I try to
    declare an iterator over the contained elements I get an error, see
    the twp uncommented lines:

    #include <set>
    template<class T>
    class MySet : public std::set<T>{
    public:
    MySet() : std::set<T>() {}
    void foo(){
    // const_iterator it; // error: `const_iterator' undeclared (first
    use this
    function)
    // std::set<T>::const_iterator it; // error: expected `;' before
    "it"
    }
    };

    I thought const_iterator should be a member of this class (since I am
    deriving from std::set). What am I doing wrong?

    Thanks!
    Markus
     
    Markus Dehmann, Jun 8, 2008
    #1
    1. Advertising

  2. Markus Dehmann

    Daniel Pitts Guest

    Markus Dehmann wrote:
    > I want to derive from std::set, like shown below. But when I try to
    > declare an iterator over the contained elements I get an error, see
    > the twp uncommented lines:
    >
    > #include <set>
    > template<class T>
    > class MySet : public std::set<T>{
    > public:
    > MySet() : std::set<T>() {}
    > void foo(){
    > // const_iterator it; // error: `const_iterator' undeclared (first
    > use this
    > function)
    > // std::set<T>::const_iterator it; // error: expected `;' before
    > "it"
    > }
    > };
    >
    > I thought const_iterator should be a member of this class (since I am
    > deriving from std::set). What am I doing wrong?
    >
    > Thanks!
    > Markus

    Why derive from std::set? This seems like a good place to use delegation
    or composition instead. What functionality do you anticipate adding to set?



    --
    Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
     
    Daniel Pitts, Jun 8, 2008
    #2
    1. Advertising

  3. On Jun 8, 8:27 am, Markus Dehmann <> wrote:
    > I want to derive from std::set, like shown below. But when I try to
    > declare an iterator over the contained elements I get an error, see
    > the twp uncommented lines:
    >
    > #include <set>
    > template<class T>
    > class MySet : public std::set<T>{
    > public:
    >   MySet() : std::set<T>() {}
    >   void foo(){
    >     // const_iterator it; // error: `const_iterator' undeclared (first
    > use this
    > function)
    >     // std::set<T>::const_iterator it; // error: expected `;' before
    > "it"
    >   }
    >
    > };
    >
    > I thought const_iterator should be a member of this class (since I am
    > deriving from std::set). What am I doing wrong?


    Not getting into why you want to derive from a standard container,
    here's how you can make it work:

    #include <set>

    template<class T>
    class MySet : public std::set<T>{
    public:
    MySet() : std::set<T>() {}
    void foo(){
    //either create typedefs to use iterator/const_iterator directly
    typedef typename std::set<T>::const_iterator const_iterator;
    typedef typename std::set<T>::iterator iterator;
    const_iterator it;
    //or use fully qualified names but will need typename
    typename std::set<T>::const_iterator it2;
    }
    };

    You may find these FAQs useful - http://www.comeaucomputing.com/techtalk/templates/
     
    Abhishek Padmanabh, Jun 8, 2008
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Peter Jansson
    Replies:
    5
    Views:
    6,440
    Ivan Vecerina
    Mar 17, 2005
  2. Vinu
    Replies:
    4
    Views:
    394
    Jim Langston
    Jul 7, 2005
  3. Vinu
    Replies:
    0
    Views:
    376
  4. Hicham Mouline
    Replies:
    2
    Views:
    541
    Jeff Flinn
    Sep 25, 2010
  5. m0shbear
    Replies:
    17
    Views:
    1,693
Loading...

Share This Page