2012-07-29 32 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中,這完全合乎邏輯。

如果您希望原始設置保持不變,請發送它的副本。

相關問題