About class constructores with templates

P

parag_paul

hi All ,

I am giving the pseudo code about the problem I am talking about .

I have a class hiearchy which looks like this.


class A;

template <class X1, class X2> class B : public A{}

template <class X3> class C: public B<Xreal1, Xreal2>;


Now is it necessary to put the following statement before the line
above like the following

template <Xreal1, Xreal2> class B;
Anyway, the line "template <class X3> class C: public B<Xreal1,
Xreal2>;" is already showing the path to this hierarhcy,
And also, why do that have to instantiate the class B when you are
talking about the class C.

Since I am getting g++ compilation errors due to it.

I am not able to give the code out as it is too sphegtti type and get
some people confused ( like I did )

The pseudo code should help I guess
 
P

Pete Becker

hi All ,

I am giving the pseudo code about the problem I am talking about .

I have a class hiearchy which looks like this.


class A;

template <class X1, class X2> class B : public A{}

template <class X3> class C: public B<Xreal1, Xreal2>;


Now is it necessary to put the following statement before the line
above like the following

template <Xreal1, Xreal2> class B;
Anyway, the line "template <class X3> class C: public B<Xreal1,
Xreal2>;" is already showing the path to this hierarhcy,
And also, why do that have to instantiate the class B when you are
talking about the class C.

Since I am getting g++ compilation errors due to it.

I am not able to give the code out as it is too sphegtti type and get
some people confused ( like I did )

The pseudo code should help I guess

No, it doesn't help, because it's not at all clear what's real and
what's pseudo, nor did you mention what the error you got was. The
obvious problems are: Xreal1 and Xreal2 are neither declared nor
defined, so you can't use those names to instantiate B. And since A is
declared but not defined, you can't use it as the name of a base class.

As ever, write a small example that produces the problem that you're
running into.
 
P

peter koch

hi All ,

I am giving the pseudo code about the problem I am talking about .

I have a class hiearchy which looks like this.

class A;

template <class X1, class X2> class B : public A{}

template <class X3> class C: public B<Xreal1, Xreal2>;

The syntax of what you attempt above is

template <class X3,class Xreal1,class Xreal2> class C: public
B said:
Now is it necessary to put the following statement before the line
above like the following

template <Xreal1, Xreal2> class B;

Yes it is - you have to declare B before you can use it!
Anyway, the line "template <class X3> class C: public B<Xreal1,
Xreal2>;" is already showing the path to this hierarhcy,
And also, why do that have to instantiate the class B when you are
talking about the class C.

Surely any instantation of class C must instantiate class B? If you
dont believe so, your expectations are wrong. You could declare the
class without instantiating it, of course:
template <Xreal1, Xreal2> class B;
template <class X3,class Xreal1,class Xreal2> class C: public
Since I am getting g++ compilation errors due to it.

I am not able to give the code out as it is too sphegtti type and get
some people confused ( like I did )

If you are confused already know, you should take it as a hint that
the code needs a rewrite. Just imagine how confused you will be if you
look at the code again in six months (not to mention if it becomes the
job of a collegue of yours).

/Peter
 
P

Pete Becker

The syntax of what you attempt above is

template <class X3,class Xreal1,class Xreal2> class C: public
B<Xreal1, Xreal2>;

Maybe. But if Xreal1 and Xreal2 are the names of types, then B<Xreal1,
Xreal2> is fine as originally written. Which is why this "pseudo-code"
is pointless.
Yes it is - you have to declare B before you can use it!

No, the template B has already been defined. Aside from the fact that
its definition isn't valid, because it says that A is a base class, but
A hasn't been defined.

And, of course, the message header says something about contructors,
although the "pseudo-code" doesn't show any constructors.

It's rarely worthwhile trying to figure out what sketches like this are
supposed to show. Which is why the FAQ says to post complete, minimal
examples. That way, people can compile the code for themselves and see
what's really going on.
 
P

parag_paul

Maybe. But if Xreal1 and Xreal2 are the names of types, then B<Xreal1,
Xreal2> is fine as originally written. Which is why this "pseudo-code"
is pointless.





No, the template B has already been defined. Aside from the fact that
its definition isn't valid, because it says that A is a base class, but
A hasn't been defined.

And, of course, the message header says something about contructors,
although the "pseudo-code" doesn't show any constructors.

It's rarely worthwhile trying to figure out what sketches like this are
supposed to show. Which is why the FAQ says to post complete, minimal
examples. That way, people can compile the code for themselves and see
what's really going on.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Sorry Pete

Here A has already defined. I am sorry I did not put that, but my
intentions were somewhere else.
Lets forget the tree from A and above


By the way, I got the purpose of declaring the line above.
Thanks to all.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,795
Messages
2,569,644
Members
45,356
Latest member
deepthi.kodakandla

Latest Threads

Top