How to resolve circular dependency of nested type in C++? -


for code like:

// forward declaration: error c2653: 'b': not class or namespace name // without forward declaration: error c2027: use of undefined type 'b' struct b;  struct {     using size_t = int;     typename b::size_t index; };  struct b {     using size_t = char;     typename a::size_t index; };  void main() {     a;     b b; } 

class , b reference nested types size_t of each other.

but forward declarations class a; works classes declared in current scope or outer scope.

so how resolve circular dependency without changing semantics of code? solving easier case forward declaration, or java , c# not kind of circular dependency problem?


p.s.

thankes galik's suggestions, original example in question

template <typename t> class {     using iterator = t*;     b<t>::iterator iter; };  template <typename t> class b {     using iterator = t*;     a<t>::iterator iter; }; 

contains other bugs iterator inaccessible private, , typename required before a<t>::iterator. , after bug fix, compiled successfully. seems there differences between generic class , normal class, gave example (of normal class) @ beginning , can not compile in visual studio 2015 @ least.

i'm not sure what's question op wants answer.
moreover, example in question contains several errors.

that said, solve maybe.

you can introduce base class 1 of 2 classes:

struct a_b {     using size_t = int; };  struct b {     using size_t = char;     a_b::size_t index; };  struct a: a_b {     b::size_t index; };  int main() {     a;     b b; } 

or turn 1 of them template class:

template<class t> struct a_t {     using size_t = int;     typename t::size_t index; };  struct b {     using size_t = char;     typename a_t<b>::size_t index; };  using = a_t<b>;  int main() {     a;     b b; } 

and on......


Comments

Popular posts from this blog

PySide and Qt Properties: Connecting signals from Python to QML -

c# - DevExpress.Wpf.Grid.InfiniteGridSizeException was unhandled -

scala - 'wrong top statement declaration' when using slick in IntelliJ -