Problem with inner classes of the same name in Visual C ++

I have a problem with Visual C ++ 2005, where the inner classes with the same name seem to be confused, but in different outer classes.

The problem arises for two layers, where each layer has a listener interface as an inner class. B is a listener of A and has its own listener in the third layer above it (not shown).

The code structure is as follows:

hijras

class A { public: class Listener { public: Listener(); virtual ~Listener() = 0; }; // ... }; 

Bh

 class B : public A::Listener { class Listener { public: Listener(); virtual ~Listener() = 0; }; // ... }; 

A :: Listener () and A :: ~ Listener () are defined in A.cpp.

B.cpp

 B::Listener::Listener() {} B::Listener::~Listener() {} 

I get an error

 B.cpp(49) : error C2509: '{ctor}' : member function not declared in 'B' 

The C ++ compiler for Renesas sh2a has no problems with this, but then it is more liberal than Visual C ++, in some other respects too.

If I rename the listener interfaces to different names, the problem will disappear, but I would like to avoid it (the names of real classes instead of A or B are quite long).

Is what I'm doing right C ++, or is it an excuse for a Visual C ++ complaint?

Is there a way around this problem without renaming the listener interfaces?

+4
source share
1 answer

The code you sent created the same compiler error as on my machine. I'm not so sure of myself that the problem is exactly, but I have the feeling that inheriting from a pure virtual class and declaring a pure virtual class inside a descendant may not be a good idea.

I managed to compile the modified version, maybe this will help you solve your problems:

 class OuterA { public: class Listener { public: Listener() {} virtual ~Listener() = 0 {} }; OuterA() {} ~OuterA(){} }; class OuterB : public OuterA::Listener { public: class Listener { public: Listener() {} ~Listener() {} }; OuterB() {} ~OuterB() {} }; 

// EDIT to avoid the built-in ctor and dtor

If you use typedefs to hide Listeners names, at least my compilations and demo code links:

// header

 class OuterA { public: class Listener { public: Listener(); virtual ~Listener() = 0; }; OuterA(); ~OuterA(); }; class OuterB : public OuterA::Listener { public: class Listener { public: Listener(); virtual ~Listener() = 0; }; OuterB(); ~OuterB(); }; 

// implementation

 OuterA::OuterA(){} OuterA::~OuterA(){} OuterA::Listener::Listener(){} OuterA::Listener::~Listener(){} typedef OuterB::Listener BListener; OuterB::OuterB() {} OuterB::~OuterB(){} BListener::Listener(){} BListener::~Listener(){} 
+1
source

Source: https://habr.com/ru/post/1309090/


All Articles