2

请帮我解决这个错误

代码:

u16 ip_defragment(){
    u16 result;
    fragip_set::iterator i;
    IP_FRAGMENTED new_defrag;
    IP* pcurpack = (IP*) malloc(cur.len);
    memcpy(pcurpack, cur.data, cur.len);
    new_defrag.saddr = cur.saddr;
    new_defrag.daddr = cur.daddr;
    new_defrag.protocol = cur.ip.ppack->protocol;
    new_defrag.id = i2i(cur.ip.ppack->id);

    i = ip_frags.find(new_defrag);

    if(i != ip_frags.end()){
            i->packets.insert(pcurpack);
            const_cast<u16&>(i->cur_len) += cur.ip.len - cur.ip.hlen; 
            const_cast<u32&>(i->last_time) = time();
            if(!(cur.ip.bmore_fr) && (i->tot_len == 0)){
            const_cast<u16&>(i->tot_len) = cur.ip.fr_offs + cur.ip.len;
            }
            if(i->cur_len == i->tot_len){
                for(set<IP*>::iterator k = i->packets.begin(); k != i->packets.end(); k++){
                    // must copy to another buffer
                    if(i2i((*k)->frag_off) & IP_OFFMASK){
                        memcpy(ip_defrag_buffer, *k, (*k)->ihl<<2);
                    } else {
                        memcpy(ip_defrag_buffer + (i2i((*k)->frag_off) & IP_OFFMASK) * 8, 
                            *k + ((*k)->ihl<<2), (i2i((*k)->tot_len))-((*k)->ihl<<2));
                    }
                }
                IP* defr_ip = (IP*) &ip_defrag_buffer;
                defr_ip->tot_len = i2i(i->tot_len);
                defr_ip->frag_off = 0;
                result = i->tot_len;
                ip_frags.erase(i);
                return result;
            }
            return 0;
    }


    if(!(cur.ip.bmore_fr)){
        new_defrag.tot_len = cur.ip.fr_offs + cur.len;
    } else {
        new_defrag.tot_len = 0;
    }
    new_defrag.cur_len = cur.ip.len; // with header size
    new_defrag.last_time = time();
    i = ip_frags.insert(new_defrag).first;
    if(i != ip_frags.end())
        i->packets.insert(pcurpack);

    return 0;
}

编译项目和查看只有2个类似的错误

第 15 行:i->packets.insert(pcurpack);

结束行:i->packets.insert(pcurpack);

2 行错误:错误 C2663:'std::_Tree<_Traits>::insert':4 个重载对 'this' 指针没有合法转换

IntelliSense: no instance of overloaded function "std::set<_Kty, _Pr, _Alloc>::insert [with _Kty=IP *, _Pr=std::less<IP *>, _Alloc=std::allocator<IP *>]" matches the argument list and object (the object has type qualifiers that prevent a match)

请帮我?

4

1 回答 1

2

在将 std::set 传递给 lambda 表达式时,我遇到了完全相同的错误:

C2663 'std::_Tree<std::_Tset_traits<_Kty,_Pr,_Alloc,false>>::insert': 5 overloads have no legal conversion for 'this' pointer

lambda 表达式原型是:

[se1, ele1](auto val) {
    /* here I was editing se1 set, but above se1 is passed value type */
}

我已更改为:

[&se1, ele1](auto val) {
    /* now since se1 set is sent as reference type above, it is good to edit
       changes stays as I expect it to be
    */
}

现在编译成功。

我使用了 count_if 函数,它为 eac 元素调用 lamda 表达式,因此编译器知道修改应该保留在 set se1 中,这是完全合乎逻辑的。

如果您希望原始设置保持不变,请发送一份副本。

于 2016-08-11T04:33:24.617 回答