Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > help on constructor/constructor initialization list with inheritanceusing templates

Reply
Thread Tools

help on constructor/constructor initialization list with inheritanceusing templates

 
 
balaji
Guest
Posts: n/a
 
      08-08-2011
Hi,

Pls find below the sample code.

File 1

template< class A, template < typename > class B >
class C : public B<A>
{
public:
explicit C(const char *msg);
~C();

private:
//copy constructor and assignment operator
}

template< class A, template < typename > class B >
C<A, B> :: C(const char *msg)
: B(msg)
{
}

template< class A, template < typename > class B >
C<A, B> :: ~C()
{
}

File 2

template < class Z>
class B : public Z
{
public:
explicit B(const char *msg);
~B();

private:
//copy constructor and assignment operator
}

template < class Z>
B<Z> :: B(const char *msg)
: Z(msg)
{
}

template < class Z>
B<Z>::~B()
{
}

File 3

3.h
class A
{
public:
explicit A(const char *msg);
~A();

private:
//copy constructor and assignment operator
}

3.C
#include "3.h"

A::A(const char *msg)
{
}

A::~A()
{
}

main file

#iinclude file 1, file 2 and file 3(.h)

int
main(int argc, char **argv)
{
C<A,B> objc("Success");
return 0;
}

when i compile this code, i get the following error "The base class
"B<A>" cannot be initialized because it does not have a default
constructor.". I do not understand what i have missed to get this
error. Pls help me to resolve this.

Thanks,
Balaji.
 
Reply With Quote
 
 
 
 
Jens Thoms Toerring
Guest
Posts: n/a
 
      08-08-2011
balaji <(E-Mail Removed)> wrote:
> Hi,


> Pls find below the sample code.


> File 1


> template< class A, template < typename > class B >
> class C : public B<A>
> {
> public:
> explicit C(const char *msg);
> ~C();


> private:
> //copy constructor and assignment operator
> }


You're missing a semicolon here (and also is some other
places at the end of declarations of classes).

> template< class A, template < typename > class B >
> C<A, B> :: C(const char *msg)
> : B(msg)


This is the place my compiler doesn't like when other,
minor issues are fixed:

error: no matching function for call to ‘B<A>::B()’
note: candidates are: B<Z>::B(const char*) [with Z = A]
note: B<A>::B(const B<A>&)

Shouldn't that be

template< class A, template < typename > class B >
C< A, B > :: C( const char * msg )
: B< A >( msg )

since B<A> is the base class C is derived from?

Otherwise I guess the compiler will try to invoke the
default constructor of B<A> (since there's no explicit
call of it - you call B(msg), not B<A>(msg)) and since
there is none it gives up.
Regards, Jens
--
\ Jens Thoms Toerring ___ http://www.velocityreviews.com/forums/(E-Mail Removed)
\__________________________ http://toerring.de
 
Reply With Quote
 
 
 
 
balaji
Guest
Posts: n/a
 
      08-10-2011
On Aug 8, 2:46*pm, (E-Mail Removed) (Jens Thoms Toerring) wrote:
> balaji <(E-Mail Removed)> wrote:
> > Hi,
> > Pls find below the sample code.
> > File 1
> > template< class A, template < typename > class B >
> > class C : public B<A>
> > {
> > * public:
> > * * explicit C(const char *msg);
> > * * ~C();
> > * private:
> > * * //copy constructor and assignment operator
> > }

>
> You're missing a semicolon here (and also is some other
> places at the end of declarations of classes).
>
> > template< class A, template < typename > class B >
> > C<A, B> :: C(const char *msg)
> > * : B(msg)

>
> This is the place my compiler doesn't like when other,
> minor issues are fixed:
>
> * error: no matching function for call to B<A>::B()
> * note: candidates are: B<Z>::B(const char*) [with Z = A]
> * note: * * * * * * * * B<A>::B(const B<A>&)
>
> Shouldn't that be
>
> template< class A, template < typename > class B >
> C< A, B > :: C( const char * msg )
> * : B< A >( msg )
>
> since B<A> is the base class C is derived from?
>
> Otherwise I guess the compiler will try to invoke the
> default constructor of B<A> (since there's no explicit
> call of it - you call B(msg), not B<A>(msg)) and since
> there is none it gives up.
> * * * * * * * * * * * * * * *Regards, Jens
> --
> * \ * Jens Thoms Toerring *___ * * *(E-Mail Removed)
> * *\__________________________ * * *http://toerring.de


Hi Jens,

Thanks for your response and sorry for the late reply as well.

My post wasn't posted atleast for 2 days i believe or atleast i was
unable to see this post.

I too figured out that. What compiler is yours to give the
"candidates". My compiler didn't say that(itx IBM xlC version 7.0).

Thanks,
Balaji.
 
Reply With Quote
 
Jens Thoms Toerring
Guest
Posts: n/a
 
      08-10-2011
balaji <(E-Mail Removed)> wrote:
> On Aug 8, 2:46*pm, (E-Mail Removed) (Jens Thoms Toerring) wrote:
> > balaji <(E-Mail Removed)> wrote:
> > > template< class A, template < typename > class B >
> > > C<A, B> :: C(const char *msg)
> > > * : B(msg)

> >
> > This is the place my compiler doesn't like when other,
> > minor issues are fixed:
> >
> > * error: no matching function for call to ‘B<A>::B()’
> > * note: candidates are: B<Z>::B(const char*) [with Z = A]
> > * note: * * * * * * * * B<A>::B(const B<A>&)
> >
> > Shouldn't that be
> >
> > template< class A, template < typename > class B >
> > C< A, B > :: C( const char * msg )
> > * : B< A >( msg )

>
> I too figured out that. What compiler is yours to give the
> "candidates". My compiler didn't say that(itx IBM xlC version 7.0).


It's gcc, version 4.4.3. Having the candidates the compiler
considered being shown can be helpful but also, at other times,
a bit annoying when the compiler found hundreds and hundreds
of them and insists on listing each and every one of them

Best regards, Jens
--
\ Jens Thoms Toerring ___ (E-Mail Removed)
\__________________________ http://toerring.de
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
initialization of array as a member using the initialization list aaragon C++ 2 11-02-2008 04:57 PM
array initialization in initialization list. toton C++ 5 09-28-2006 05:13 PM
how to Specializations of function Templates or Overloading Function templates with Templates ? recover C++ 2 07-25-2006 02:55 AM
Initialization of non-integral type in initialization list anongroupaccount@googlemail.com C++ 6 12-11-2005 09:51 PM
Templates templates templates JKop C++ 3 07-21-2004 11:44 AM



Advertisments