-1

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>

....
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • 3
    You need to forward declare in the same namespace. – François Andrieux Sep 10 '18 at 18:51
  • Thank you for your help. I'm sorry, but i don't understand what you mean. Can you explain it to me on this example please? – StackTourista Sep 10 '18 at 19:49
  • I can't see, how i'm in a different namespace. – StackTourista Sep 10 '18 at 20:01
  • Just an FYI, there is a much shorter `TMREWSync` typedef available for `TMultiReadExclusiveWriteSynchronizer` – Remy Lebeau Sep 11 '18 at 02:13
  • "*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*" - because the compiler is not compiling the HPP file itself, it is compiling the CPP file. So it first includes your HPP and parses it, then it includes `vcl.h` and parses it, which includes `System.SysUtils.hpp` (amongst others). – Remy Lebeau Sep 11 '18 at 02:17
  • From the description of the problem, it's very likely you are trying to forward declare `::System::Sysutils::TMultiReadExclusiveWriteSynchronizer` and that you are actually forward declaring `::TMultiReadExclusiveWriteSynchronizer`. – François Andrieux Sep 11 '18 at 13:14

1 Answers1

1

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.

François Andrieux
  • 28,148
  • 6
  • 56
  • 87
jsantander
  • 4,972
  • 16
  • 27