Reputation: 26
In the simplified code below I am trying within VS to not have an error occur at the typedef statement in that it doesn't recognize B at that statement and will give the error "missing ';' before '<'".
template<class DT>
class A{
typedef B<DT> iterator;
}
template<class DT>
class B{
friend A<DT>;
}
And I understand that inserting a class prototype of B should solve this problem (source: Class prototyping) but I am still finding that VS doesn't recognize that B exists before entering the typedef statement, and will still spit back that same syntax error
class B; //prototype of B
template<class DT>
class A{
typedef B<DT> iterator;
}
template<class DT>
class B{
friend A<DT>;
}
And in the case of switching the arrangement to having B assigned first then A, and changing it to having a class prototype of A, because A is supposed to be a friend of B, but the compiler doesn't know B exists yet. I get the error of having "argument list for class template A is missing".
class A; //prototype of B
template<class DT>
class B{
friend A<DT>;
}`
template<class DT>`
class A{
typedef B<DT> iterator;
}
And looking into that error (source: Argument list for class template is missing) I found you change the statement to taking a '' and adding the statement 'template' above that. Which does solve the inheritance between the two classes, but that causes the methods (within my detailed code) of our class A to be inaccessible when testing the functionality. Any ideas as to how I should go about keeping this inheritance the same but not making class A become purely virtual, non accessible from another source (.cpp)?
Upvotes: 1
Views: 107
Reputation: 26
What I found in my detailed code was that I had added unnecessary instances of <DT>
when defining my classes, i.e. template <class DT> class AbstractLinkedList<DT> { ... }
and so when trying to do as Ishmael suggested and as Andrey commented making the full template class prototype, the compiler was only recognizing the first instance and not the second which snowballed other errors. In taking out those extra <DT>
's the class inheritance agreed, so going with the simplified code used in my question, my working code is along the lines of
template<class DT> class A;
template<class DT>
class B{
friend A<DT>;
};
template<class DT>
class A{
typedef B<DT> iterator;
};
Upvotes: 0
Reputation: 12795
This should work for you:
template<class DT> class B;
template<class DT>
class A{
typedef B<DT> iterator;
};
template<class DT>
class B{
friend A<DT>;
};
Upvotes: 1