Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > derive from std::set, const_iterator does not work

Reply
Thread Tools

derive from std::set, const_iterator does not work

 
 
Markus Dehmann
Guest
Posts: n/a
 
      06-08-2008
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
 
Reply With Quote
 
 
 
 
Daniel Pitts
Guest
Posts: n/a
 
      06-08-2008
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/>
 
Reply With Quote
 
 
 
 
Abhishek Padmanabh
Guest
Posts: n/a
 
      06-08-2008
On Jun 8, 8:27*am, Markus Dehmann <markus.dehm...@gmail.com> 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/
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
GeneratorExit should derive from BaseException, not Exception Chad Austin Python 0 08-20-2007 10:00 PM
Derive or not to bobsled C++ 2 04-28-2004 11:38 AM
Not able to derive from abstract class Amil ASP .Net 1 11-20-2003 11:16 AM
const_iterator john smith C++ 5 08-05-2003 10:35 AM
why is map::begin() returning const_iterator? Tim Partridge C++ 5 07-24-2003 08:21 PM



Advertisments