By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,420 Members | 1,133 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,420 IT Pros & Developers. It's quick & easy.

Problem with base constructors using templates

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a

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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.