Visual C 中同名内部类的问题++

发布于 2024-08-31 11:04:50 字数 966 浏览 1 评论 0原文

我在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

铁轨上的流浪者 2024-09-07 11:04:50

您发布的代码产生了您在我的机器上描述的相同编译器错误。我自己不太确定问题到底是什么,但我有一种感觉,从纯虚拟类继承并在后代中声明纯虚拟类可能不是一个好主意。

我设法编译了一个修改版本,也许这可以帮助您解决您的问题:

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() {}
};

// 编辑以避免内联 ctor 和 dtor

如果您使用 typedef 来隐藏侦听器的名称,至少我的演示代码会编译并链接:

// 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();
};

//执行

OuterA::OuterA(){}
OuterA::~OuterA(){}

OuterA::Listener::Listener(){}
OuterA::Listener::~Listener(){}

typedef OuterB::Listener BListener;

OuterB::OuterB() {}
OuterB::~OuterB(){}

BListener::Listener(){}
BListener::~Listener(){}

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:

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 inline ctor and dtor

If you use typedefs to hide the names of the Listeners at least my demo code compiles and 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(){}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文