473,511 Members | 16,110 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problem with base constructors using templates

The code below fails to compile with VC7.1 and GCC 3.2.3, when the line
labelled BAD is used, instead of the line immediately after it. The error
from VC7.1 is "no appropriate default constructor available". However, it
will compile with VC8, so is the BAD line valid C++?

Thanks,
Keith MacDonald

template<typename T>
class Base
{
public:
Base(T n)
: val_(n)
{}

T val_;
};

template<typename T>
class Derived
: public Base<T>
{
public:
Derived(T n)
// BAD : Base(n)
: Base<T>(n)
{}
};

int main()
{
Derived<int> d(7);
return 0;
}
Jul 23 '05 #1
2 1354

I'm no VC-Pro, but the BAD line looks bad to me:-)
The reason is, that you only instanciated n as of type T
inside the "Derived(T n)", so you have to write
"Base<T>(n)" in order to instanciate the typename of the
Base class.
Keith MacDonald wrote:
The code below fails to compile with VC7.1 and GCC 3.2.3, when the line
labelled BAD is used, instead of the line immediately after it. The error
from VC7.1 is "no appropriate default constructor available". However, it
will compile with VC8, so is the BAD line valid C++?

Thanks,
Keith MacDonald

template<typename T>
class Base
{
public:
Base(T n)
: val_(n)
{}

T val_;
};

template<typename T>
class Derived
: public Base<T>
{
public:
Derived(T n)
// BAD : Base(n)
: Base<T>(n)
{}
};

int main()
{
Derived<int> d(7);
return 0;
}

Jul 23 '05 #2
I tried it with gcc 3.4.1 (Cygwin version) and the error I got from the
compiler was:
class `Derived<T>' does not have any field named `Base'
This error seems to me a bit more meaningful than the one you get from
the VC compiler. I reckon the gcc compler doesn't even bother looking
at the constructor of the base class as a possible candidate because
the template parameter is not specified.

Jul 23 '05 #3

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

Similar topics

7
1615
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'...
4
1384
by: Gama Franco | last post by:
Hi, I've been developing this API, but now I get stuck in a compiling error and I'm out of ideas. Some comments are welcome. The hierarchy is bases in three classes, and I will explain it...
7
2117
by: Lionel B | last post by:
Greetings. The following code compiles ok and does what I'd expect it to do: ---------- START CODE ---------- // test.cpp
4
1905
by: MrTang001 | last post by:
How I can fix this problem? I don't know why it alway prompted (first use this function). I have use newDollars, newCents... to access the base class member variable. And the member functions for...
19
2198
by: Robbie Hatley | last post by:
For your enjoyment, a function that expresses any integer with absolute value less-than-or-equal-to nine quintillion in any base from 2 to 36. (For larger bases, you could expand the "digit"...
4
1555
by: srinivasarao_moturu | last post by:
class ABC { public : virtual void operation1(); virtual void operation2(); virtual int GetValue(); virtual char GetValue(); virtual void SetValue(int); virtual void SetValue(char); }
1
1719
by: connor7777 | last post by:
Hi guys: We've been weeding out errors off of a java->c# project and have managed to redeem most of our code with the exception of one bug that we for some reason cannot pin down. The following...
19
1902
by: Juha Nieminen | last post by:
Assume I have a class Base, and then this: struct Derived: public Base { AnotherClass member; }; Also assume that I'm allocating an object of type 'Derived' with a custom allocator and...
6
1350
by: Peng Yu | last post by:
Hi, I want B has all the constructors that A has. Obviously, the code below would not work. I could define a corresponding B's constructor for each A's constructor. But if A has many...
0
7242
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7353
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,...
1
7075
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
7508
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
4737
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3222
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...
0
3212
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
781
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
446
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.