# 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.

## 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 on other sites
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 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 on other sites
Quote:
 Original post by uavfunYou may also have to use B* instead of just B*.

strike tags added by me. Same applies to A: A* -> A<T>*

##### Share on other sites
Quote:
 Original post by MaulingMonkeyYou also have to use B* instead of just B*

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

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 on other sites
Quote:
Original post by Timkin
Quote:
 Original post by MaulingMonkeyYou also have to use B* instead of just B*

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

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

EDIT: Nevermind.

##### Share on other sites
Quote:
 Original post by joanusdmentiatemplate class B

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 on other sites
Yeah, I just double checked and realized I was wrong [smile]

##### Share on other sites
Quote:
Original post by Timkin
Quote:
 Original post by MaulingMonkeyYou also have to use B* 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.

• 21
• 16
• 9
• 17
• 13
×

## Important Information

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!