Circular class dependency... how to resolve?

Started by
8 comments, last by MaulingMonkey 19 years, 6 months ago
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
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*.
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.
Quote:Original post by uavfun
You may also have to use B<T>* instead of just B*.


strike tags added by me. Same applies to A: A* -> A<T>*
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
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
EDIT: Nevermind.
"Voilà! In view, a humble vaudevillian veteran, cast vicariously as both victim and villain by the vicissitudes of Fate. This visage, no mere veneer of vanity, is a vestige of the vox populi, now vacant, vanished. However, this valorous visitation of a bygone vexation stands vivified, and has vowed to vanquish these venal and virulent vermin vanguarding vice and vouchsafing the violently vicious and voracious violation of volition. The only verdict is vengeance; a vendetta held as a votive, not in vain, for the value and veracity of such shall one day vindicate the vigilant and the virtuous. Verily, this vichyssoise of verbiage veers most verbose, so let me simply add that it's my very good honor to meet you and you may call me V.".....V
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
Yeah, I just double checked and realized I was wrong [smile]
"Voilà! In view, a humble vaudevillian veteran, cast vicariously as both victim and villain by the vicissitudes of Fate. This visage, no mere veneer of vanity, is a vestige of the vox populi, now vacant, vanished. However, this valorous visitation of a bygone vexation stands vivified, and has vowed to vanquish these venal and virulent vermin vanguarding vice and vouchsafing the violently vicious and voracious violation of volition. The only verdict is vengeance; a vendetta held as a votive, not in vain, for the value and veracity of such shall one day vindicate the vigilant and the virtuous. Verily, this vichyssoise of verbiage veers most verbose, so let me simply add that it's my very good honor to meet you and you may call me V.".....V
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.

This topic is closed to new replies.

Advertisement