Visual C 中同名内部类的问题++
我在使用 Visual C++ 2005 时遇到问题,其中具有相同名称但位于不同外部类中的内部类显然会混淆。
该问题发生在两层上,其中每层都有一个侦听器接口作为内部类。 B 是 A 的侦听器,并且在其上方的第三层中拥有自己的侦听器(未显示)。
代码结构如下:
Ah
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() 和 A::~Listener() 定义在 A.cpp 中。
B.cpp
B::Listener::Listener() {}
B::Listener::~Listener() {}
我收到错误
B.cpp(49) : error C2509: '{ctor}' : member function not declared in 'B'
Renesas sh2a 的 C++ 编译器对此没有问题,但在其他方面它也比 Visual C++ 更自由。
如果我将侦听器接口重命名为不同的名称,问题就会消失,但我想避免这种情况(真正的类名而不是 A 或 B 相当长)。
我所做的是正确的 C++,还是 Visual C++ 的投诉是合理的?
有没有办法在不重命名侦听器接口的情况下解决此问题?
I have a problem with Visual C++ 2005, where apparently inner classes with the same name but in different outer classes are confused.
The problem occurs 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 a third layer above it (not shown).
The structure of the code looks like this:
A.h
class A
{
public:
class Listener
{
public:
Listener();
virtual ~Listener() = 0;
};
// ...
};
B.h
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 the error
B.cpp(49) : error C2509: '{ctor}' : member function not declared in 'B'
The C++ compiler for Renesas sh2a has no problem with this, but then it is more liberal than Visual C++ in some other respects, too.
If I rename the listener interfaces to have different names the problem goes away, but I'd like to avoid that (the real class names instead of A or B are rather long).
Is what I'm doing correct C++, or is the complaint by Visual C++ justified?
Is there a way to work around this problem without renaming the listener interfaces?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您发布的代码产生了您在我的机器上描述的相同编译器错误。我自己不太确定问题到底是什么,但我有一种感觉,从纯虚拟类继承并在后代中声明纯虚拟类可能不是一个好主意。
我设法编译了一个修改版本,也许这可以帮助您解决您的问题:
// 编辑以避免内联 ctor 和 dtor
如果您使用 typedef 来隐藏侦听器的名称,至少我的演示代码会编译并链接:
// header
//执行
the code you posted produced the same compiler error you described on my machine. I'm not so sure myself what the problem exactly is, but I have a feeling that inherting from a pure virtual class and declaring a pure virtual class within the descendant might not be a good idea.
I managed to compile a modified version, maybe this helps you solve your problems:
// EDIT to avoid inline ctor and dtor
If you use typedefs to hide the names of the Listeners at least my demo code compiles and links:
// header
// implementation