Jump to content
  • Advertisement
Sign in to follow this  
Mantear

C++ template inheritance question

This topic is 3013 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
Advertisement
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
Your code compiles fine for me on MSVS 2008.

Are you sure that's an accurate example? Do you have include guards in those files?

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!