c++ - Circular Dependency with Inherited Template Class -


I am having a circular dependency problem Basically I have two classes, the first template class is my second class Uses some functionality from The second class comes from my template class.

Below is a simplified structure:

  // Foo.h #pragma once #include "Bar.h" template & lt; Class T & gt; Class Fu {Public: Zero DoSomething (); }; Template & lt; Class T & gt; Zero Foo & lt; T & gt; :: DoSomething () {Bar :: GetInstance () -> LogEvent (); } // Burgus # Pragma Once #include "Foo.h" class bar: public Foo < Bar & gt; {Public: Fixed Times * GetInstance (); Zero log event (); }; //Bar.cpp #include "Bar.h" times * GetInstance () {// Return of Singleton Class Return Example} zero times :: LogEvent () {// log in some event in a file} < / Code> 

The problem is now that when I configure the code I get the following errors in twelve

  Bar.h (): Error C2504: 'Foo': Base Class Undefined Bar.h (): Error C2143: Syntax Error: Missing ',' First '& lt;'  

I can definitely tell it the dependency problem. If I remove the call from 'DoSomething' to 'LogEvent', and removes the reference "Bar.h" from Foo.h, the problem gets resolved.

Although this is not really a solution because Fu requires functionality with the bar, on the contrary, the strip obtained from the foo gets and it needs to include reference to Foo.h it occurs.

So - how can I solve this problem? I have noticed other posts about circular references, but I have not been able to solve the problem.

Thanks

Depending on the code that you posted, foo.h Does not need to include Bar.h, and if it is not, then the problem will be solved.

The important thing is that the compiler sees Foo after seeing the bar, and this can not happen when #include is at the top of "bar.h" foo.h ( Which depends on the order foo.h and bar.h < #include ad) in the consumer module. P>

He said, if a base class needs to take a specific derivative class and call it by name, it seems that maybe there is a design problem (will the base function have a virtual function?) . But I can not make that decision without seeing the whole picture.

Based on the comments given below, I think you can solve the immediate problem by adding a third file:

  // LogEvent.h zero log event ( ...); // LogEvent.cpp #include "bar.h" zero log event (...) {time :: GetInstance () - & gt; Log Avent (...); }  

then modify foo.h as follows:

  // Include a time # "LogEvent.h" template & lt; Class T & gt; Class Fu {Public: Zero DoSomething (); }; Template & lt; Class T & gt; Zero Foo & lt; T & gt; :: DoSomething () {LogEvent (...); }  

Unless other problems are there, you should at least compile it.


Comments

Popular posts from this blog

import - Python ImportError: No module named wmi -

Editing Python Class in Shell and SQLAlchemy -

c# - MySQL Parameterized Select Query joining tables issue -