derive from std::set, const_iterator does not work

M

Markus Dehmann

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
 
D

Daniel Pitts

Markus said:
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?
 
A

Abhishek Padmanabh

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/
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,961
Messages
2,570,131
Members
46,689
Latest member
liammiller

Latest Threads

Top