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;
};
Circular class dependency... how to resolve?
Okay, I have something like this
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
Put
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*.
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 TimkinQuote: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
Quote:Original post by joanusdmentiatemplate <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
Quote:Original post by TimkinQuote: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
Popular Topics
Advertisement