C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

template problems.

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
benjamin malaussene
Guest





PostPosted: Mon Jun 26, 2006 9:10 am    Post subject: template problems. Reply with quote



Hi everybody.
I am now studying C++ and even if I like to do OOP, I have some
problems Smile
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(){
Iterator<T> itr(this);
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;
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.

Thanx in advance


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Martin Bonner
Guest





PostPosted: Tue Jun 27, 2006 4:18 pm    Post subject: Re: template problems. Reply with quote



benjamin malaussene wrote:
Quote:
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.
Welcome to the wonderful world of programming! Replace "template

class" with "code" and you have the story of the last twenty five years
of my life :-)

Quote:
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(){
Iterator<T> itr(this);
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;
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.

In general, it is always a good idea to include the compiler error
messages in a posting like this, but in this particular case I am
pretty sure I know what it is complaining about.

Quote:
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 is the line
Iterator GetIterator();
Iterator is a template, but the compiler wants a specific class. It
doesn't know whether you mean Iterator<int> or Iterator<void *> or
what. You need to tell it. (Hint. You get it right in the body of
the member function.)


The next problem has nothing to do with your class being a template.
If you write:

struct bert {};

struct fred {
bert & GetBert() { bert b; return b; }
};

You will have the same problem as you have in your GetIterator
function. What does the result of GetBert refer to? Where is that
object?


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
kanze
Guest





PostPosted: Tue Jun 27, 2006 4:26 pm    Post subject: Re: template problems. Reply with quote



benjamin malaussene wrote:

Quote:
I am now studying C++ and even if I like to do OOP, I have
some problems Smile
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.

Quote:
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.

Quote:
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.

Quote:
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! ]
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.