473,883 Members | 1,683 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Invoking templatized base class constructor from templatized derived class constructor

Ok, I've got code that looks something like this:

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

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

template<typena me 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 7379
mrstephengross wrote:
Ok, I've got code that looks something like this:

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

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

template<typena me 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<typena me T1, typename T2>
class Base
{
public:
explicit Base(const T1 & t1) { /* ... */ }
};

template<typena me 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
1991
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 try to compile it with gcc, I instead get the compiler error message
7
1636
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' copy constructor Calling 'func(d_handle)':
3
3502
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; // ... more protected fields }
12
5824
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. The subclass's constructor sets this variable to a real value. So first the variable is set to 0, the variable is never used before it once again gets set to a real value. It's a waste. Any ideas anyone?
4
5253
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 attempt to access the object -- since m_object is not actually created and initizialized until after the base constructor has been called. Any thoughts on the practice below? class Base { public:
6
2505
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
3447
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 static constructor of the base class would be invoked even if a an object of the derived class is created. Is this correct ? Is there any way to ensure the base class's static constructor is invoked before the derived class instance is constructed ?...
3
2259
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
5385
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 because I am not always able to control/create all the different constructors the base class has. My problem can be described in code as follows ... /* This is the base class with a whole heap of constructors/functionality*/ public class Animal
0
9791
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10742
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10847
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10414
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7971
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5797
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5991
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4611
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4218
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.