i have two templated classes. they both have pointers to each other. but
since they accept a different set of template parameters each, a simple declaration
of the other before the first won't help me this time.
lets say:
//class Y.. bla bla.
template<class T, class E, int i> class X { Y<..>* y; ... };
template<class T, int i> class Y { X<...>* x; ... };
but the parameters for X are unknown to Y.
im sure there's quite a simple solution. lets hear you :>
Sefi.
Brian Preston
12-26-2001, 10:00 AM
Your forward delaration would be.
template<class T, int i> class Y;
Brian
"Sefi P." <dioscuri100@hotmail.com> wrote:
>
>ok, here's an easy snack, guys.
>
>i have two templated classes. they both have pointers to each other. but
>since they accept a different set of template parameters each, a simple
declaration
>of the other before the first won't help me this time.
>
>lets say:
>//class Y.. bla bla.
>template<class T, class E, int i> class X { Y<..>* y; ... };
>template<class T, int i> class Y { X<...>* x; ... };
>
>but the parameters for X are unknown to Y.
>
>im sure there's quite a simple solution. lets hear you :>
>
>Sefi.
>
>
Sefi P.
12-26-2001, 02:36 PM
"Brian Preston" <brian81@yahoo.com> wrote:
>
>Your forward delaration would be.
>
>template<class T, int i> class Y;
>
>Brian
no, it wouldn't work. i tried that. i was just leaving it unexplained in
the example code. of course I used the parameters. the deal is that X doesn't
recognize class E implicitly (which appears in ptr to Y there..)
hope you got me now.
Sefi.
Sefi P.
12-27-2001, 06:31 AM
Danny Kalev <dannykk@inter.net.il> wrote:
>Brian is right. This code compiled and links without a hitch:
>
>template<class T, int i> class Y;
>template<class T, class E, int i> class X
>{ Y<int, 5>* y; };
>
>template<class T, int i> class Y { X<char, int, 5>* x;};
>
>int main()
>{
> X<int,int,5> x;
>}
>
>If it still refuses to compile on your IDE, your template probably have
>more than mere pointers to other templates. In that case, you want show
>us more of your code.
>
>Danny
uh-oh...
well. i didn't specify concrete classes in my classes definitions.
itr goes more like:
template<class T, int i> class Y { X<T, E, int>* x;};
(hence is the problem E is not recognized in scope of Y (Y has a pointer
to X<T,E,int>* x)
why would i use a template class that has data memebers always created with
the same nametypes? where is the arbitrariness? I may be totally wrong, but
- isn't it the whole idea about templates?
you can write a data memeber that is of the type specified in the template
parameters, and create a difference instance each time.
I wish to write a data memeber that is a pointer to another template class,
but *this* template class itself does not need to know it (because it's a
pointer to a class, it doesn't need to know details.)
I hope i explained myself more brightly this time.
finally - the code.
template<class T, class E, int i>
class Automata;
template<class T,/*class E,*/ int i>
class State {
it doesn't work unless E is added to State definition...
thank you for bothering. (and please explain, if you can, why the compiler
won't be content with the forward declaration, since he can see the whole
thing is a pointer - why does it "on the spot" need to find out what are
the types passed to the pointer? )
Sefi (that finally descended to the silliest level possible :>).
Danny Kalev
12-27-2001, 06:35 AM
Brian is right. This code compiled and links without a hitch:
template<class T, int i> class Y;
template<class T, class E, int i> class X
{ Y<int, 5>* y; };
template<class T, int i> class Y { X<char, int, 5>* x;};
int main()
{
X<int,int,5> x;
}
If it still refuses to compile on your IDE, your template probably have
more than mere pointers to other templates. In that case, you want show
us more of your code.
Danny
"Sefi P." wrote:
>
> ok, here's an easy snack, guys.
>
> i have two templated classes. they both have pointers to each other. but
> since they accept a different set of template parameters each, a simple declaration
> of the other before the first won't help me this time.
>
> lets say:
> //class Y.. bla bla.
> template<class T, class E, int i> class X { Y<..>* y; ... };
> template<class T, int i> class Y { X<...>* x; ... };
>
> but the parameters for X are unknown to Y.
>
> im sure there's quite a simple solution. lets hear you :>
>
> Sefi.
Danny Kalev
12-27-2001, 11:56 AM
"Sefi P." wrote:
>
> Danny Kalev <dannykk@inter.net.il> wrote:
> >Brian is right. This code compiled and links without a hitch:
> >
> >template<class T, int i> class Y;
> >template<class T, class E, int i> class X
> >{ Y<int, 5>* y; };
> >
> >template<class T, int i> class Y { X<char, int, 5>* x;};
> >
> >int main()
> >{
> > X<int,int,5> x;
> >}
> >
> >If it still refuses to compile on your IDE, your template probably have
> >more than mere pointers to other templates. In that case, you want show
> >us more of your code.
> >
> >Danny
>
> uh-oh...
> well. i didn't specify concrete classes in my classes definitions.
> itr goes more like:
> template<class T, int i> class Y { X<T, E, int>* x;};
It seems like you're missing a crucial point here, namely the
distinction between a template parameter and a template argument. The
former is an arbitrary symbol, say T, which needs to be declared as such
by a preceding 'class' or 'typename' keyword:
template <class T, class Z> x; // T and Z are template parameters.
A template argument is an actual type: char, double, bool, std::string
*or* a constant value (say 5, &C::f, 'c', true and so on). You can't
just throw in an arbitrary symbol as a template argument. A template
argument is the actual type that the template parameter represents and
you use it when you instantiate a template (or a pointer or a reference
to a template). In this case, the argument list must contain previously
declared types, built-in types or constants. Think of a fwd declaration
as the templates' equivalent of a function prototype: the latter must
have the exact number of parameters and their types. Likewise, a pointer
to a template requires a previous declaration of that template,
including all of its parameters/arguments.
>
> (hence is the problem E is not recognized in scope of Y (Y has a pointer
> to X<T,E,int>* x)
>
> why would i use a template class that has data memebers always created with
> the same nametypes?
Because the names are then substituted for actual types. The compiler
maps each symbol to an matching argument and performs a substitution
when a template specialization is instantiated.
>where is the arbitrariness?
There's no arbitrariness. The template parameters represent actual types
that at some point must be specified by the programmer. There is
genericity, i.e., type independence, but not arbitrariness -- certainly
not when it comes to the number of arguments that a template takes. A
template is a mold from which similar classes and functions are created;
it's not an amorphous entity (as opposed to void *) from which you can
create a specialization with any number of arguments.
I may be totally wrong, but
> - isn't it the whole idea about templates?
> you can write a data memeber that is of the type specified in the template
> parameters, and create a difference instance each time.
Yes, but the parameter must be declared beforehand. Remember that two
template declarations are independent of one another. Thus in:
template <class T> x;
template <class T> y;
The T in the first declaration and the T in the second one needn't refer
to the same actual type (although they may). In fact, the code above is
equivalent to:
template <class T> x;
template <class U> y;
>
> I wish to write a data memeber that is a pointer to another template class,
> but *this* template class itself does not need to know it (because it's a
> pointer to a class, it doesn't need to know details.)
When you create a pointer as a data member, it has to be a pointer to a
previously declared type or a built-in type. Likewise, the template must
be declared before you can create a pointer to it. The only thing that
you can postpone is the *definition* of the template.
>
> I hope i explained myself more brightly this time.
>
> finally - the code.
>
> template<class T, class E, int i>
> class Automata;
>
> template<class T,/*class E,*/ int i>
> class State {
>
> State* transition[i];
> vector<T*> stateDataVector;
> Automata<T,E,i>* machine;
>
> public:
> ...... (not anything that specifically mentions the class E)...
> };
>
> template<class T, class E, int i>
> class Automata {
>
> AlephBet<E, i>* alephBet;
> State<T,/*E,*/i>* initialState;
>
> public:
> .....
> };
>
> it doesn't work unless E is added to State definition...
I hate to say this, but the solution is that simple, why not go for
it?:)
>
> thank you for bothering. (and please explain, if you can, why the compiler
> won't be content with the forward declaration, since he can see the whole
> thing is a pointer - why does it "on the spot" need to find out what are
> the types passed to the pointer? )
The compiler knows it's a pointer -- there's no arguing about that.
However, pointers in C++ are strongly typed. Hence, their types must be
known or previously declared. This means that in:
> class State {
>
> Automata<T,E,i>* machine;
The definition of Automata<T,E,i> (i.e., the class body, the signatures
of member functions and the data members) isn't mandatory yet whereas a
declaration of Automata<T,E,i> is. Put differently, at this point E must
recognized by the compiler as a previously declared token.
Danny
devx.com
Copyright Internet.com Inc. All Rights Reserved