473,408 Members | 2,813 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

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 (mr************@hotmail.com)

Jul 23 '05 #1
5 7283
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
Jul 23 '05 #2
Thanks!

--Steve

Jul 23 '05 #3
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.
Jul 23 '05 #4
>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

Jul 23 '05 #5
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.
Jul 23 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Christian Engström | last post by:
When I compile the below program with Microsoft Visual C++ version 6, I get the results I expect, which is that the program should write out base() derived() base() derived(derived) When I...
7
by: Christian Engström | last post by:
When i compile the program listed below with gcc version 3.3.1 (MinGW on Windows XP) I get the following result: Calling 'func(d)': 'base' copy constructor Calling 'func(*d_handle)': 'base'...
3
by: J.J. Feminella | last post by:
(Please disregard the previous message; I accidentally sent it before it was completed.) I have source code similar to the following. public class Vehicle { protected string dataV; // ......
12
by: st_ev_fe | last post by:
I've noticed that when constructing a subclass, the base class get's it's contructors called. Is there some way to avoid this? The base class has one variable, which gets initialised to 0. ...
4
by: Jeff | last post by:
The derived class below passes a reference to an object in its own class to its base calss constructor. The code compiles and will run successfully as long as the base class constructor does not...
6
by: Taran | last post by:
Hi All, I tried something with the C++ I know and some things just seem strange. consider: #include <iostream> using namespace std;
12
by: Hemanth | last post by:
Hi, I have a base class with a static constructor and some abstract methods. Derived classes implement these methods. From articles on the web, it appears that there is no guarentee that this...
3
by: hurcan solter | last post by:
Consider the code fragment; class A { public: A(){} A(int prm):mprm(prm){} int mprm; }; class B:public A {
26
by: nyathancha | last post by:
Hi, How Do I create an instance of a derived class from an instance of a base class, essentially wrapping up an existing base class with some additional functionality. The reason I need this is...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.