Invoking templatized base class constructor from templatized derived class constructor

M

mrstephengross

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 ([email protected])
 
V

Victor Bazarov

mrstephengross said:
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 said:
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
 
L

Larry I Smith

Victor said:
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
 
M

mrstephengross

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
 
L

Larry I Smith

mrstephengross said:
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
 

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,733
Messages
2,569,440
Members
44,830
Latest member
ZADIva7383

Latest Threads

Top