Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   derive from std::set, const_iterator does not work (http://www.velocityreviews.com/forums/t619065-derive-from-std-set-const_iterator-does-not-work.html)

Markus Dehmann 06-08-2008 03:27 AM

derive from std::set, const_iterator does not work
 
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

Daniel Pitts 06-08-2008 04:24 AM

Re: derive from std::set, const_iterator does not work
 
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/>

Abhishek Padmanabh 06-08-2008 06:16 AM

Re: derive from std::set, const_iterator does not work
 
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/


All times are GMT. The time now is 12:37 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.