这个错误是因为在调用opApply时,传递给委托的参数类型不匹配。在foreach中,k和v都被声明为引用,因此在调用委托时,需要使用相应的引用类型。但是,在opApply的声明中,委托的参数类型是ref K和ref V,这意味着我们需要传递一个引用类型的参数。因此,我们需要对opApply的声明进行修改,以接受const K和const V类型的参数:

int opApply(int delegate(ref const K, ref V) dg)
{
    int result;
    foreach (k, ref v; data) {
        result = dg(k, v);
        if (result) break;
    }
    return result;
}

int opApply(int delegate(ref const V) dg)
{
    int result;
    foreach (ref v; data.values) {
        result = dg(v);
        if (result) break;
    }
    return result;
}

int opApply(int delegate(ref const K) dg)
{
    int result;
    foreach (ref k; data.keys) {
        result = dg(k);
        if (result) break;
    }
    return result;
}

这样,我们就可以正确地使用foreach遍历MyMap对象了

我有以下dlang语言代码编译报错:f_range_forach自定义遍历增强d24 Error delegate dgref string ref int is not callable using argument types conststring intf_range_forach自定义遍历增强d24 cannot pass argument k of type consts

原文地址: https://www.cveoy.top/t/topic/fF7A 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录