Sign in to follow this  

C++ template inheritance question

This topic is 2826 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Greetings, I'm trying to get the correct syntax for inheriting a template class. For example, say I have the following classes:
template <typename T>
class Base
{
   T baseMember;
};

template <typename X>
class Derived : public Base<X> // <--- error point A
{
   X derivedMember
}; // <--- error point B
Attempting to compile this with MSVS 2008 and I get the following error: [error point A] "error C2143: syntax error : missing ',' before '<'" [error point B] "see reference to class template instantiation 'Derived<X>' being compiled" I'm no template wiz, so can someone point out what I'm doing wrong? Thanks.

Share this post


Link to post
Share on other sites
For one you are missing a ; after X derivedMember other than that it works for me on GCC.

In the original code are you sure the base template is visible to the derived template?

Share this post


Link to post
Share on other sites
Yes, I had a typo in my copy-and-pasted example. Here's a slightly update version.

// File Base.hpp
template <typename T>
class Base
{
T baseMember;
};

// File Derived.hpp
#include <Base.hpp>

template <typename X>
class Derived : public Base<X> // <--- error point A
{
X derivedMember;
}; // <--- error point B


Share this post


Link to post
Share on other sites
Sign in to follow this