【问题标题】:Workaround for a Visual studio 2005 c++ inheritance bugVisual Studio 2005 C++ 继承错误的解决方法
【发布时间】:2012-11-07 11:14:05
【问题描述】:

以下代码在 Visual Studio 2005 中无法编译:

class OriginalClass
{
public:
    class Delegate
    {
        virtual void original_func()=0;
    };
};

class BaseClass
    :public OriginalClass::Delegate //  Problem line 1
{
public:
    class Delegate
    {
        virtual void base_func(int x) = 0;
    };

    void original_func()override{}  //  Problem line 2
};

class DerivedClass : public BaseClass::Delegate
{
public:
    virtual void base_func(int x) override {};
};

int main ()
{
    DerivedClass derived_object;

    derived_object.base_func(10);
}

构建输出是:

1>inherit\main.cpp(26) : error C3668: 'DerivedClass::base_func' : method with override specifier 'override' did not override any base class methods
1>inherit\main.cpp(32) : error C2259: 'DerivedClass' : cannot instantiate abstract class
1>        due to following members:
1>        'void OriginalClass::Delegate::original_func(void)' : is abstract
1>        inherit\main.cpp(7) : see declaration of 'OriginalClass::Delegate::original_func'

如果我注释掉标记为 Problem line 1Problem line 2 的行,它就可以构建。因此,使用 override 不是问题,从嵌套类继承也不是问题。似乎很难确定哪个 Delegate 类是正确的。

VC2008不存在这个问题。

任何人都可以为此提出解决方法吗?除了显而易见的:升级到现代编译器!

另外,如果有人能指出错误的任何文档(如果它是错误的话),我将不胜感激。

编辑: @Anonymous Coward 建议使用 typedefs,如果 OriginalClass 更改为以下内容,它将编译:

class OriginalClass
{
public:
    class Delegate_t
    {
        virtual void original_func()=0;
    };
    typedef Delegate_t Delegate;
};

【问题讨论】:

  • 不能使用其他编译器的原因究竟是什么?错误本身很清楚编译器有什么问题,您有任何其他证据,这是编译器错误,而不是编译器缺少此功能的事实?
  • @Ramhound 我需要坚持使用 2005 的原因是我需要支持旧的 wince 设备,为此我只有 2005 SDK。我不知道这是否是一个错误,但我仍然需要以某种方式编译这段代码。

标签: c++ inheritance nested-class visual-c++-2005


【解决方案1】:

这似乎确实是一个名称解析问题。它在使用 typedefs 时编译:

class OriginalClass {
    // class Delegate { ... };
    typedef Delegate delegate_t;
};

class BaseClass : public OriginalClass::delegate_t {
    // class Delegate { ... };
    typedef Delegate delegate_t;
};

class DerivedClass : public BaseClass::delegate_t {
    // ...
};

【讨论】:

  • 太棒了,谢谢。您只需要更改 OriginalClass::Delegate 的名称和 typedef 即可使其工作。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-02-10
  • 2017-09-02
  • 2010-12-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多