Search code examples
c++c++11c++builderforward-declaration

Name conflict in forward declaration


I declare a pointer of type TMultiReadExclusiveWriteSynchronizer in my header. For this I want to use a forward declaration

class TMultiReadExclusiveWriteSynchronizer;

Because the vcl.h library where this class is described is already included in the cpp file.

But if I do this like in the example below, I get an error:

ambigious name

because the compiler finds the name TMultiReadExclusiveWriteSynchronizer in my forward declaration and in System::Sysutils::TMultiReadExclusiveWriteSynchronizer (which is part of vcl.h).

Without my forward declaration, the compiler tells me this type is unknown. I do not understand, why the compiler can't find the TMultiReadExclusiveWriteSynchronizer class when I don't use a forward declaration, but if I use one then the compiler has a name conflict.

How can it even find the name in System::Sysutils::TMultiReadExclusiveWriteSynchronizer? This is not included in my header. Like in my example below, my hpp file has no includes.

How can I fix this problem?

Sure, I could just include System::Sysutils in my header, or change the order of the includes in my cpp. But I don't want to include something, when I only need a simple forward declaration.

file.hpp

class TMultiReadExclusiveWriteSynchronizer;

....

TMultiReadExclusiveWriteSynchronizer* sync;

file.cpp

#include "file.hpp"
#include <vcl.h>

....

Solution

  • In file.hpp, the forward declaration should be:

    namespace System {
        namespace Sysutils {
             class TMultiReadExclusiveWriteSynchronizer;
        }
    }
    

    And probably all references to the type (at least in the header file) should have the fully qualified name System::Sysutils::TMultiReadExclusiveWriteSynchronizer.