 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Avikam Guest
|
Posted: Sun Oct 09, 2005 8:24 pm Post subject: Self defined datatype with templates |
|
|
// iter.h
class Iter
{
public:
virtual void f() = 0;
};
// cont.h
template <class T>
class Cont
{
public:
struct ADT
{
T data;
ADT * next;
};
/// ... dealing with container
private:
ADT * head;
};
// con_tter.h
#include "cont.h"
template <class T>
class IterCont : public Iter<T>
{
Cont<T> & container;
Cont<T>::ADT * curr; // PROBLEM!!
public:
IterCont (Cont<T> c) : Iter<T>(), container (c) {}
};
///////////////
in other words... I tried to implement a database using this sketch.
the thing is that i'm having a problem using the ADT data type that was
defined in the Cont class, in the Iterator class.
The compailer (g++) won't let me declate in class IterCont a datatype
that was declared in another class.
what can I do??
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Yuri Khan Guest
|
Posted: Mon Oct 10, 2005 10:07 am Post subject: Re: Self defined datatype with templates |
|
|
Avikam wrote:
| Quote: | template <class T
class Cont
{
public:
struct ADT
{
T data;
ADT * next;
};
/// ... dealing with container
private:
ADT * head;
};
// con_tter.h
#include "cont.h"
template
class IterCont : public Iter
{
Cont
Cont<T>::ADT * curr; // PROBLEM!!
|
At this point, the compiler does not yet know that Cont<T>::ADT will be
a type. If somebody specializes Cont for their own type and redefine
ADT, it might as well be a data member, a static data member, a member
function, or a static member function, or something else.
You must tell your compiler that ADT will be a type:
typename Cont<T>::ADT* curr; // no problem
| Quote: | public:
IterCont (Cont<T> c) : Iter<T>(), container (c) {}
};
|
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Daniel Krügler Guest
|
Posted: Mon Oct 10, 2005 10:08 am Post subject: Re: Self defined datatype with templates |
|
|
Avikam wrote:
| Quote: | // iter.h
class Iter
{
public:
virtual void f() = 0;
};
|
Following your latter code it seems as if you meant
template <typename T>
class Iter
{
public:
virtual void f() = 0;
};
instead. Right?
| Quote: | // cont.h
template <class T
class Cont
{
public:
struct ADT
{
T data;
ADT * next;
};
/// ... dealing with container
private:
ADT * head;
};
// con_tter.h
#include "cont.h"
|
I assume a missing
#include "iter.h"
here.
| Quote: | template
class IterCont : public Iter
{
Cont
Cont<T>::ADT * curr; // PROBLEM!!
typename Cont<T>::ADT * curr; // no problem any more
public:
IterCont (Cont<T> c) : Iter<T>(), container (c) {}
};
what can I do??
|
Add the missing typename as specified above. The compiler can't know
whether Cont<T>::ADT is a type or a variable. It seems obvious for you,
but you have to consider, that one could easily specialize the class
template Cont such that ADT specifies a static member and not a type:
class Wonder1;
template <>
class Cont<Wonder1>
{
public:
static int ADT;
};
class Wonder2;
template <>
class Cont<Wonder2>
{
public:
enum { ADT };
};
Greetings from Bremen,
Daniel Krügler
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Valentin Samko Guest
|
Posted: Mon Oct 10, 2005 10:13 am Post subject: Re: Self defined datatype with templates |
|
|
Avikam wrote:
| Quote: | // iter.h
class Iter
{
public:
virtual void f() = 0;
};
// cont.h
template <class T
class Cont
{
public:
struct ADT
{
T data;
ADT * next;
};
/// ... dealing with container
private:
ADT * head;
};
// con_tter.h
#include "cont.h"
template
class IterCont : public Iter
What is Iter |
| Quote: | {
Cont<T> & container;
Cont<T>::ADT * curr; // PROBLEM!!
It should be typename Cont<T>::ADT, otherwise the compiler does not |
know at this point whether Cont<T>::ADT is a type or not.
| Quote: | public:
IterCont (Cont<T> c) : Iter<T>(), container (c) {}
Again, no such thing as Iter
};
|
--
Valentin Samko - http://val.samko.info
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Alberto Ganesh Barbati Guest
|
Posted: Mon Oct 10, 2005 10:15 am Post subject: Re: Self defined datatype with templates |
|
|
Avikam wrote:
| Quote: |
// con_tter.h
#include "cont.h"
template <class T
class IterCont : public Iter
{
Cont
Cont<T>::ADT * curr; // PROBLEM!!
public:
IterCont (Cont<T> c) : Iter<T>(), container (c) {}
};
///////////////
in other words... I tried to implement a database using this sketch.
the thing is that i'm having a problem using the ADT data type that was
defined in the Cont class, in the Iterator class.
The compailer (g++) won't let me declate in class IterCont a datatype
that was declared in another class.
what can I do??
|
The compiler complains because ADT is a dependent name and so, by
default, it refers to a non-type. In order to make the compiler
understand that it's a type you have to use the typename keyword:
class IterCont : public Iter<T>
{
Cont<T> & container;
typename Cont<T>::ADT * curr; // OK
// ^^^^^^^^
public:
IterCont (Cont<T> c) : Iter<T>(), container (c) {}
};
For more info, have a look at the very first FAQ here:
http://womble.decadentplace.org.uk/c++/template-faq.html
HTH,
Ganesh
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Greg Herlihy Guest
|
Posted: Mon Oct 10, 2005 11:11 am Post subject: Re: Self defined datatype with templates |
|
|
Avikam wrote:
| Quote: | // iter.h
class Iter
{
public:
virtual void f() = 0;
};
|
In order for the code below to compile, I'm assuming that the Iter
class is really meant to be a class template:
template <class T>
class Iter
{
public:
virtual void f() = 0;
};
| Quote: | // cont.h
template <class T
class Cont
{
public:
struct ADT
{
T data;
ADT * next;
};
/// ... dealing with container
private:
ADT * head;
};
// con_tter.h
#include "cont.h"
template
class IterCont : public Iter
{
Cont
Cont<T>::ADT * curr; // PROBLEM!!
public:
IterCont (Cont<T> c) : Iter<T>(), container (c) {}
};
|
Since curr is a pointer to - and not an instance of - a Cont<T>::ADT
object, it is not necessary to provide ADT's full declaration in order
for curr's declaration to compile. Instead it is necessary only to
inform that compiler what kind of type curr is pointing to. Since we
happen to know that ADT is a class type, the fix is to also let the
compiler know that ADT is the name of a class:
template <class T>
class IterCont : public Iter<T>
{
Cont<T> & container;
class Cont<T>::ADT * curr; // problem solved
public:
IterCont (Cont<T> c) : Iter<T>(), container (c) {}
};
| Quote: | ///////////////
in other words... I tried to implement a database using this sketch.
the thing is that i'm having a problem using the ADT data type that was
defined in the Cont class, in the Iterator class.
The compailer (g++) won't let me declate in class IterCont a datatype
that was declared in another class.
what can I do??
|
In general, always try to limit the amount of external information
brought into a header file to just the minimal amount needed for it to
compile. For example, prefer forward declarations and elaborated type
specifiers over full declarations when identifying types named in a
header file. The less that each header file knows about other header
files, the fewer the dependencies that will be created between header
files and the fewer the number of headaches that will be suffered by
anyone maintaining or improving the code in the future.
Greg
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
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
|
|