Board index » cppbuilder » friend ???

friend ???


2005-07-12 04:29:45 AM
cppbuilder97
A reproduction of code [minimal part for error]
(actually i didn't test this reproduction because i think i should add other
things,
like constructors).
#include <list>
using namespace std;
template<class T>
class TMyClass{
public:
typedef typename list<T>::const_iterator const_iterator;
class TMyConstIterator : private const_iterator{
private:
const TMyClass *Owner;
friend TMyClass;
public:
TMyConstIterator & operator ++(){
if (static_cast<const_iterator&>(*this) !=
Owner->elements.end())
static_cast<const_iterator&>(*this)->operator++();
};
};
private:
list<T>elements;
friend TMyConstIterator;
};
Wht the compiler get me errore on 'friend TMyConstIterator' instruction?
Thanks.
 
 

Re:friend ???

"mauro russo" < XXXX@XXXXX.COM >wrote in the message
Quote
Wht the compiler get me errore on 'friend TMyConstIterator' instruction?
the error is [translaction]
[C++ Error] intervalli.h(449): E2404 Dependent type qualifier
'TMyClass<TClassOfTemplate>' has no member type named 'TMyConstIterator'
 

Re:friend ???

"mauro russo" < XXXX@XXXXX.COM >writes:
Quote
#include <list>
using namespace std;

template<class T>
class TMyClass{
public:
typedef typename list<T>::const_iterator const_iterator;
class TMyConstIterator : private const_iterator{
private:
const TMyClass *Owner;
friend TMyClass;
public:
TMyConstIterator & operator ++(){
if (static_cast<const_iterator&>(*this) !=
Owner->elements.end())
static_cast<const_iterator&>(*this)->operator++();
};
};
private:
list<T>elements;
friend TMyConstIterator;
};
When you declare something to be a friend, it is either a function or
a class. For a class, you need to tell it that:
friend class TMyClass; // inside TMyConstIterator
and
friend class TMyConstIterator; // in TMyClass
Also, notice that your operator++ doesn't ever return anything. Be
sure to handle the case when the condition is false, too!
--
Chris (TeamB);
 

{smallsort}

Re:friend ???

"mauro russo" < XXXX@XXXXX.COM >writes:
Quote
"mauro russo" < XXXX@XXXXX.COM >wrote in the message
news:42d2d6d6$ XXXX@XXXXX.COM ...
>Wht the compiler get me errore on 'friend TMyConstIterator' instruction?

the error is [translaction]

[C++ Error] intervalli.h(449): E2404 Dependent type qualifier
'TMyClass<TClassOfTemplate>' has no member type named 'TMyConstIterator'
It looks like line 449 of intervalli.h is not among the code you posted.
Please the minimal program (<50 lines) that causes the compiler to
produce this error message.
 

Re:friend ???

"Chris Uzdavinis (TeamB)" < XXXX@XXXXX.COM >wrote in the message
Quote
"mauro russo" < XXXX@XXXXX.COM >writes:

When you declare something to be a friend, it is either a function or
a class. For a class, you need to tell it that:

friend class TMyClass; // inside TMyConstIterator
The compiler didn't give me error here: why?
Quote
friend class TMyConstIterator; // in TMyClass
thanks, but why the compiler writes me:
[C++ Error] MyFile.h(449): E2404 Dependent type qualifier
'TMyClass<TClassOfTemplate>' has no member type named 'TMyConstIterator'
It confused me, and let me understand it knows can be a 'type name'.
I mean: what it wrote it's a false (or not?) because there is a type with
that name!
Anyway..... with your adjustement, it works, thank you.
Quote
Also, notice that your operator++ doesn't ever return anything. Be
sure to handle the case when the condition is false, too!
Well, it's like list<...>::iterator::operator++(), right?
The problem is that i prefer to return *this.
Mauro Russo.
 

Re:friend ???

"Thomas Maeder [TeamB]" < XXXX@XXXXX.COM >wrote in the message
Quote
It looks like line 449 of intervalli.h is not among the code you posted.

Please the minimal program (<50 lines) that causes the compiler to
produce this error message.
Sorry, i forgot to translate that part of error message.
In my first post there was the minimal code, that i couldn't test by
compiler,
otherwise i had to add not little code.
 

Re:friend ???

"mauro russo" < XXXX@XXXXX.COM >writes:
Quote
"Chris Uzdavinis (TeamB)" < XXXX@XXXXX.COM >wrote in the message
news: XXXX@XXXXX.COM ...
>"mauro russo" < XXXX@XXXXX.COM >writes:
>

>When you declare something to be a friend, it is either a function or
>a class. For a class, you need to tell it that:
>
>friend class TMyClass; // inside TMyConstIterator

The compiler didn't give me error here: why?
I don't know, since the answer depends on the compiler implementation.
But I think it should.
Quote

>friend class TMyConstIterator; // in TMyClass

thanks, but why the compiler writes me:
[C++ Error] MyFile.h(449): E2404 Dependent type qualifier
'TMyClass<TClassOfTemplate>' has no member type named 'TMyConstIterator'
line 449?? I think you're compiling different code than you're
posting. Please don't do that. Post nice small examples, but explain
and test the problem against the code you post, not against the
application from which you were inspired to write the post.
Quote
>Also, notice that your operator++ doesn't ever return anything. Be
>sure to handle the case when the condition is false, too!

Well, it's like list<...>::iterator::operator++(), right?
No. That function does indeed return an iterator.
Quote
The problem is that i prefer to return *this.
That's a bad design because the iterator may not be dereferencable.
Then what do you do? Not returning a value when a function interface
says it should is undefined behavior. Returning an invalid object is
undefined behavior.
Thus, returning a reference is a bad decision unless you can GUARANTEE
that EVERY call will return reference to a valid object. And in this
case, that is something you cannot guarantee.
--
Chris (TeamB);
 

Re:friend ???

"mauro russo" < XXXX@XXXXX.COM >writes:
Quote
"Thomas Maeder [TeamB]" < XXXX@XXXXX.COM >wrote in the message
news: XXXX@XXXXX.COM ...

>It looks like line 449 of intervalli.h is not among the code you posted.
>
>Please the minimal program (<50 lines) that causes the compiler to
>produce this error message.

Sorry, i forgot to translate that part of error message.
In my first post there was the minimal code, that i couldn't test by
compiler,
otherwise i had to add not little code.
That's not a really good answer, though, because you're asking us to
work with the code you posted, which wastes our time if it doesn't
represent your real problem.
Furthermore, with very few tweaks, I could compile the code you posted
with Comeau C++.
--
Chris (TeamB);