Search code examples
c++visual-c++gccforward-declarationcircular-dependency

interdependent classes in same namespace problem


I'm in a real fix... I need to port code, which has a lot of interdependent classes and uses namespaces in order to avoid includes. This works in MSVC, but I can't find a way to deal with this situation in GCC :(

Contents of myString.h file:

#include "baseBuffer.h"
//I can't forward declare a base class, so I have to include its header

namespace test
{
    class MY_ALLOCATOR
    {
        static const unsigned int LIMIT = 4096;
        class myBuffer : public BaseBuffer<LIMIT>
        {
//...
        }
    };

    template <class T, typename ALLOC = MY_ALLOCATOR> class myContainer
    {
//...
    }

    typedef myContainer<char> myString;
}


Contents of baseBuffer.h file:

#include "myObject.h"
//#include "myString.h"
//I can't include **myString.h**, because it includes this header file and I can't seem to find a way to use forward declaration of **myString** class...

namespace test
{
    template <uint limit> class BaseBuffer : public MyObject
    {
        public:
            myString sData;
//...
    }
}


Please help!


Solution

  • You are missing guards in your header files.

    MSVC most likely allows you to do that through an extension. Therefore, there are two ways how to solve this :
    1. the 1st solution is to merge those two headers.
    2. the 2nd solution is forward declare template class myContainer, and create it dynamically in the basebuffer.hpp (instead of myString sData, create myString *sData)

    EDIT Add a cpp file for baseBuffer, and include that instead of the header file. In the header forward declare template class myString, and in the source you can include whatever you like.