Jump to content
  • Advertisement
Sign in to follow this  
Timkin

Circular class dependency... how to resolve?

This topic is 5407 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

Okay, I have something like this
template <typename T>
class A
{
  public:
    A(B* b,T t);
};

template <typename T>
class B
{
  public:
    B();
// ... other methods involving T
  private:
    A* a;
};


How, if at all, can one forward declare a template class? Am I forced to nest A within B? I originally used this nested object model but I felt I didn't really want A to be nested within B, so I'm trying to separate the two classes... but I have found this circular dependency issue. Any help is appreciated. Thanks, Timkin

Share this post


Link to post
Share on other sites
Advertisement
Put
template <typename T> class B;

Before the definition of A. It's just like for any other class/struct but you have to specify that it's a template.

You may also have to use B<T>* instead of just B*.

Share this post


Link to post
Share on other sites
Terminology: it's called a forward reference. C and C++ require forward declaration of all entities, so this is a valuable feature. A function prototype (including method declarations in a header) is an example of a forward reference.

Share this post


Link to post
Share on other sites
Quote:
Original post by MaulingMonkey
You also have to use B<T>* instead of just B*


Sorry, that was just a typo/unintentended ommision on my part...

I had tried using
template <typename T> class B

but I was getting errors when compiling... it must have been something else causing it... I'll go back and check it more carefully.

Thanks,

Timkin

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Quote:
Original post by Timkin
Quote:
Original post by MaulingMonkey
You also have to use B* instead of just B*


Sorry, that was just a typo/unintentended ommision on my part...

I had tried using
template class B
but I was getting errors when compiling... it must have been something else causing it... I'll go back and check it more carefully.

Thanks,

Timkin

Actually, he was referring to uavfun's post. You should use
template class B

Share this post


Link to post
Share on other sites
Quote:
Original post by joanusdmentia
template <typename T> class B<T>


Actually MSVC 7.1 complains (throws and error: error C2143: syntax error : missing ';' before '<') if I use that, but doesn't complain
if I omit the <T> after the class tag.

I got it to work... I was having trouble with templated friend functions and that was interfering with my forward declarations (and I was probably getting some errors mixed up). Anyway, all working now. Thanks for the help.

Cheers,

Timkin

Share this post


Link to post
Share on other sites
Quote:
Original post by Timkin
Quote:
Original post by MaulingMonkey
You also have to use B<T>* instead of just B*


Sorry, that was just a typo/unintentended ommision on my part...


No need to appologize - I figured as much, I just wanted to clear up uavfun's post for any other readers of this topic.

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!