Re: template problems.
benjamin malaussene wrote:
I am now studying C++ and even if I like to do OOP, I have
some problems :)
I will explain my problem.
I have to do a program for my System programming course. and I don't
know why but my template class doesnt work.
Goal of the template classes (there are two classes actually).
I have to create a Collection class and an iterator class both generic.
Here is what I have done.
template <class T>
class Collection{
public:
Collection(){
Head=NULL;
pos=NULL;
len=0;
}
bool Add(T newObject){
/*this function seems to work and is quiet long*/
}
int Size(){return len;}
Iterator &GetIterator(){
Exactly what is Iterator? The C++ compiler doesn't look ahead
(at least not infinitly), and the C++ language requires things
to be defined, or at least declared, before they are used.
Iterator<T> itr(this);
Same problem here, only worse. In the absense of a visible
template declaration, the compiler assumes that < is the less
than operator.
return itr;
}
private:
T &Next(){
if(pos==NULL)
return NULL;
if(pos->next ==NULL)
return pos->object;
return pos->next->object;
}
struct Element{
Element *next;
T object;
};
int len;
Element pos;
Element *Head;
friend bool Iterator<T>::HasNext();
friend T& Iterator<T>::Iterator(Collection<T>);
};
template<class T>class Iterator{
public:
Iterator(Collection<T> col){
myCollection.Head=col.Head;
myCollection.size=col.size;
begin=true;
}
bool HasNext(){
if(myCollection.Head.next !=NULL)
return true;
return false;
}
T &Next(){
if(HasNext())
return myCollection.next();
}
private:
Iterator();
Collection<T> myCollection;
I'm almost sure you don't mean this. You can't really want the
iterator to contain a complete deep copy of the Collection.
bool begin;
};
#endif
The first and main problem I have is in the GetIterator
function. The compiler says things that make me think I simply
didn't use the declarations right. Someone can help me ?
I just don't know how to define as result type a class from a
template and it seems that is not the right way.
The problem isn't really related to templates (and you should be
able to write this sort of code before starting on templates).
What you have is a classical case of cyclic class references.
To solve it, you generally start with a foreward reference of
the classes involved:
class Collection ;
class Iterator ;
Then comm the class definitions. Obviously, at this point, you
can only do things in the class which don't require a complete
definition of the class. Thus, you can declare a pointer to the
class, or a return value or a function parameter, but you can't
declare an instance of the class, nor a definition of the
function returning an instance of the class. Finally, you
provide the definitions of the member functions.
The only way template classes differ in this regard is that all
of the definitions and declarations are templates, e.g.:
template< typename T >
class Collection ;
template< typename T >
class Iterator ;
// ...
and that the function definitions will be in the header, or in a
file included from the header, and not in a classical source
file.
(I might add that I can't figure out from the posted code what
sort of container this is, nor how the iterator is supposed to
work. Maybe looking at the code for add would help, but
somehow, I suspect that there are other, deep misunderstandings
on your part. You do understand the difference between value
semantics and reference semantics, I hope.)
--
James Kanze GABI Software
Conseils en informatique orient?e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S?mard, 78210 St.-Cyr-l'?cole, France, +33 (0)1 30 23 00 34
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]