![]() |
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) |
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 |
Re: Invoking templatized base class constructor from templatized derived class constructor
Thanks!
--Steve |
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. |
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 |
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.