locked
C2143 / circular reference error RRS feed

  • Question

  • I have a master class that is used to submit jobs for thread pool / async operation.    I built a friend / helper class that does the actual work.   It appears that since the two depend on references to each other, I encounter C2143 error at compile time.  Here is a mock up of the code:

    1 #include "FileExporter.h"

    //JobManager.h  
    2 class JobManager  
    3 {  
    4 private:  
    5     friend class FileExporter;   
    6       
    7     void ExportFile(char* file);  //FileExporter calls this private member of JobManager to do the actual work
    8  
    9 public:  
    10     void SubmitJob(FileExporter exporter);   //submit job to thread pool for async operation  
    11  
    12 };  
    13  
    14  
    15 //IRunnable.h  
    16 class IRunnable  
    17 {  
    18    virtual void Run() = 0;  
    19    virtual Dispose() = 0;  
    20 };  
    21  
    22  
    23 //FileExporter.h  
    24 #include "IRunnable.h"
    #include "JobManager.h"
    class FileExporter : public IRunnable  
    25 {  
    26 private:  
    27     JobManager* m_pManager;    //houston we have a problem, error C2143  
    28     char* m_filePath;  
    29  
    30 public:  
        void FileExporter(JobManager* manager, char* filePath)
        {
            m_pManager = manager;
            
            //deep copy the passed in string
        }
    31     void Run()  
    32     {  
    33       //we are friend, invoke private method
          m_pManager->ExportFile(m_filePath);
    34     }  
    35  
    36     void Dispose()  
    37     {  
            //release dynamic string
    38     }  
    39  
    40 }; 

    Is there an easy way around this error? 

    thanks!

    Friday, July 11, 2008 4:22 PM

Answers

  • You have two easy options, either will work:

    1: Have JobManager::SubmitJob take a FileExporter& instead of a FileExporter; don't have JobManager.h include FileExporter.h.

    2: Don't have FileExporter.h include JobManager.h; instead, make a forward declaration of JobManager inside of FileExporter.h before the FileExporter declaration ('class JobManager;'); this option will require making FileExporter's member function definitions seperate (non-inline) in a .cpp.
    • Marked as answer by scott_m Friday, July 11, 2008 7:24 PM
    Friday, July 11, 2008 6:31 PM