Sign in to follow this  
Mantear

C++ template inheritance question

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this