2012-07-29 36 views
0

请帮我这个错误错误C2663:重载有this指针没有法律转换

代码:

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 :: _树< _Traits> ::插入':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) 

没有法律转换帮帮我吗?

+0

听起来像'i->数据包'有问题。也许它是'const'? – 2012-07-29 12:32:23

+1

看起来像'i'是一个指向const对象的迭代器。因此,不要试图用const_cast强制任何东西,你应该问自己为什么这种方法是错误的。 – jahhaj 2012-07-29 12:40:57

+1

确定看起来更接近我想我可以看到有什么不对。 'ip_frags'是'std :: set'类型。方式设置的工作是一旦一个项目已被添加到一个集合,你不能修改该项目。这就是你想要做的,编译器正确地阻止你。我建议你首先从集合中删除项目,对项目进行更改,然后将其添加回集合。 – jahhaj 2012-07-29 12:50:28

回答

0

我有性病::一套完全相同的错误,而将它传递给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中,这完全合乎逻辑。

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

相关问题