Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   Invoking templatized base class constructor from templatized derived class constructor (http://www.velocityreviews.com/forums/t291153-invoking-templatized-base-class-constructor-from-templatized-derived-class-constructor.html)

mrstephengross 05-18-2005 05:10 PM

Invoking templatized base class constructor from templatized derived class constructor
 
Ok, I've got code that looks something like this:

==================================================

template<typename T1, typename T2>
class Base
{
public:
explicit Base(const T1 & t1) { /* ... */ }
};

template<typename T1, typename T2, typename T3>
class Derived : public Base<T1, T2>
{
public:
Derived() : Base(my_t3) { /* ... */ }

private:
T3 my_t3;
};

================================================== ==

GCC 3.3.1 reports the following error:

In constructor `Derived<T1, T2, T3>::Derived()':
error: class `Derived<T1, T2, T3>' does not have any field named
`template<class T1, T2> class Base'

It would seem that GCC interprets my initialization of Base in
Derived's constructor as a field assignment. Is my syntax wrong? Is
there some way to more directly indicate that I'm invoking Base's
constructor?

Thanks,
--Steve (mrstephengross@hotmail.com)


Victor Bazarov 05-18-2005 05:21 PM

Re: Invoking templatized base class constructor from templatizedderived class constructor
 
mrstephengross wrote:
> Ok, I've got code that looks something like this:
>
> ==================================================
>
> template<typename T1, typename T2>
> class Base
> {
> public:
> explicit Base(const T1 & t1) { /* ... */ }
> };
>
> template<typename T1, typename T2, typename T3>
> class Derived : public Base<T1, T2>
> {
> public:
> Derived() : Base(my_t3) { /* ... */ }


Has to be

Derived() : Base<T1,T2>(my_t3) { }

or you need to typedef 'Base<T1,T2>' as "Base" before.

>
> private:
> T3 my_t3;
> };
>
> ================================================== ==
>
> GCC 3.3.1 reports the following error:
>
> In constructor `Derived<T1, T2, T3>::Derived()':
> error: class `Derived<T1, T2, T3>' does not have any field named
> `template<class T1, T2> class Base'
>
> It would seem that GCC interprets my initialization of Base in
> Derived's constructor as a field assignment. Is my syntax wrong?


Yes.

> Is
> there some way to more directly indicate that I'm invoking Base's
> constructor?


See above.

V

mrstephengross 05-18-2005 05:34 PM

Re: Invoking templatized base class constructor from templatized derived class constructor
 
Thanks!

--Steve


Larry I Smith 05-18-2005 05:35 PM

Re: Invoking templatized base class constructor from templatizedderived class constructor
 
Victor Bazarov wrote:
> mrstephengross wrote:
>> Ok, I've got code that looks something like this:
>>
>> ==================================================
>>
>> template<typename T1, typename T2>
>> class Base
>> {
>> public:
>> explicit Base(const T1 & t1) { /* ... */ }
>> };
>>
>> template<typename T1, typename T2, typename T3>
>> class Derived : public Base<T1, T2>
>> {
>> public:
>> Derived() : Base(my_t3) { /* ... */ }

>
> Has to be
>
> Derived() : Base<T1,T2>(my_t3) { }


That's the correct syntax, but isn't there a logic flaw
in the Base() call? The Base() constructor expects a
'T1' parameter, rather than a 'T3' parameter.

[snip]

Regards,
Larry

--
Anti-spam address, change each 'X' to '.' to reply directly.

mrstephengross 05-18-2005 05:50 PM

Re: Invoking templatized base class constructor from templatized derived class constructor
 
>That's the correct syntax, but isn't there a logic flaw
in the Base() call? The Base() constructor expects a
'T1' parameter, rather than a 'T3' parameter.

Yeah, you're right... I didn't paste in my example correctly. But the
main issue has been resolved: apparently C++ requires that you
explicitly specify the constructor's template arguments. I had been
using a compiler (KCC) that was more forgiving...

--Steve


Larry I Smith 05-18-2005 07:12 PM

Re: Invoking templatized base class constructor from templatizedderived class constructor
 
mrstephengross wrote:
>>That's the correct syntax, but isn't there a logic flaw

> in the Base() call? The Base() constructor expects a
> 'T1' parameter, rather than a 'T3' parameter.
>
> Yeah, you're right... I didn't paste in my example correctly. But the
> main issue has been resolved: apparently C++ requires that you
> explicitly specify the constructor's template arguments. I had been
> using a compiler (KCC) that was more forgiving...
>
> --Steve
>


"Base<T1, T2>" IS the class name. "Base" is something
entirely different. g++ is merely enforcing the standard.

BTW, you do know that you compile/link C++ code with the GCC
command "g++", NOT with the command "gcc"???

Regards,
Larry

--
Anti-spam address, change each 'X' to '.' to reply directly.


All times are GMT. The time now is 04:43 AM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57