Connecting Tech Pros Worldwide Forums | Help | Site Map

another visibility problem

Imre Palik
Guest
 
Posts: n/a
#1: Nov 24 '05
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
n2xssvv g02gfr12930
Guest
 
Posts: n/a
#2: Nov 24 '05

re: another visibility problem


Imre Palik wrote:[color=blue]
> 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[/color]
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
Maxim Yegorushkin
Guest
 
Posts: n/a
#3: Nov 24 '05

re: another visibility problem



Imre Palik wrote:[color=blue]
> 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?[/color]

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;

Closed Thread