为什么我得到 C2883:函数声明与 - 在仅覆盖基类中的一个重载时通过使用 - delcaration 引入

Why do I get C2883: function declaration conflicts with - introduced by using-delcaration when overriding only one overload from a base class?

本文关键字:一个 重载 delcaration 引入 C2883 函数 声明 覆盖 基类 为什么      更新时间:2023-10-16

为什么下面的代码给我一个编译器错误?

struct lol {
    void foo(int hi) { }
    void foo(lol x) { }
};
void funcit() {
    struct duh : public lol {
        using lol::foo;
        void foo(lol x) { }
    };
    lol().foo(10);
    lol().foo(lol());
    duh().foo(10);
    duh().foo(lol());
}
int main() {
    funcit();
    return 0;
}

我希望它能够编译,其中duh::foo会调用lol::foo - 仅覆盖其中一个重载。使用Visual Studio Express 2012,我收到此错误:

error C2883: 'funcit::duh::foo' : function declaration conflicts with 'lol::foo' introduced by using-declaration

代码是正确的,可以使用GCC 4.8.1进行编译。这似乎是 MSVC 2012 中的一个错误。将结构从函数中移除将导致其正常工作:

struct lol {
    void foo(int hi) { }
    void foo(lol x) { }
};
namespace { 
    struct duh : public lol {
        using lol::foo;
        void foo(lol x) { }
    };
}
void funcit() {
    lol().foo(10);
    lol().foo(lol());
    duh().foo(10);
    duh().foo(lol());
}
int main() {
    funcit();
    return 0;
}
相关文章: