another visibility problem

I

Imre Palik

Hi,

for this code:

template <typename content, typename ptr, typename ref>
class tree_iterator
: public std::iterator<std::forward_iterator_tag, content, std::ptrdiff_t,
ptr, ref>
{
node<content> *node_;
public:
reference operator*() const {return node_->value;}
pointer operator->() const {return &(node_->value);}
};

gcc says

error: ISO C++ forbids declaration of `reference' with no type
error: expected `;' before "operator"
error: expected `;' before "pointer"
error: ISO C++ forbids declaration of `pointer' with no type
error: expected `;' before "operator"
error: expected `;' before '}' token

but as far as I understand, this is the intended usage of std::iterator,
and those names should be visible. Throwing using declarations at the problem
doesn't seems to help either. Any ideas what am I doing wrong?

Thx

ImRe
 
N

n2xssvv g02gfr12930

Imre said:
Hi,

for this code:

template <typename content, typename ptr, typename ref>
class tree_iterator
: public std::iterator<std::forward_iterator_tag, content, std::ptrdiff_t,
ptr, ref>
{
node<content> *node_;
public:
reference operator*() const {return node_->value;}
pointer operator->() const {return &(node_->value);}
};

gcc says

error: ISO C++ forbids declaration of `reference' with no type
error: expected `;' before "operator"
error: expected `;' before "pointer"
error: ISO C++ forbids declaration of `pointer' with no type
error: expected `;' before "operator"
error: expected `;' before '}' token

but as far as I understand, this is the intended usage of std::iterator,
and those names should be visible. Throwing using declarations at the problem
doesn't seems to help either. Any ideas what am I doing wrong?

Thx

ImRe
You haven't shown the template for node so I took the liberty of
guessing. Anyway with gcc 3.3 the following appears to compile with no
warnings or errors.

template <typename T>
class node
{
public:
T value;
};

template <typename content, typename ptr, typename ref>
class tree_iterator
: public std::iterator<std::forward_iterator_tag, content,
std::ptrdiff_t,
ptr, ref>
{
node<content> *node_;
public:
typename tree_iterator<content,ptr,ref>::reference operator*()
const {return node_->value;}
typename tree_iterator<content,ptr,ref>::pointer operator->() const
{return &(node_->value);}
};

Hope this helps

JB
 
M

Maxim Yegorushkin

Imre said:
Hi,

for this code:

template <typename content, typename ptr, typename ref>
class tree_iterator
: public std::iterator<std::forward_iterator_tag, content, std::ptrdiff_t,
ptr, ref>
{
node<content> *node_;
public:
reference operator*() const {return node_->value;}
pointer operator->() const {return &(node_->value);}
};

gcc says

error: ISO C++ forbids declaration of `reference' with no type
error: expected `;' before "operator"
error: expected `;' before "pointer"
error: ISO C++ forbids declaration of `pointer' with no type
error: expected `;' before "operator"
error: expected `;' before '}' token

but as far as I understand, this is the intended usage of std::iterator,
and those names should be visible. Throwing using declarations at the problem
doesn't seems to help either. Any ideas what am I doing wrong?

This has beed discussed many times here and elsewhere.

reference and pointer are typedefs from a template argument dependent
base class. To use them you need to fully qualify them otherwise they
will never be found by a compiler. Add the following typedefs to your
derived class:

typedef std::iterator<std::forward_iterator_tag, content,
std::ptrdiff_t, ptr, ref> base;
typedef typename base::reference reference;
typedef typename base::pointer pointer;
 

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
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top