Problem with base constructors using templates

K

Keith MacDonald

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;
}
 
M

Mario

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.
 
Y

yvinogradov

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.
 

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

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,906
Latest member
SkinfixSkintag

Latest Threads

Top