2012-07-22 18 views
2

我越來越錯誤,如果任何人有任何想法,請幫助我。錯誤:錯誤C2663:'std :: _樹<_Traits> ::插入':2重載沒有合法的轉換爲'這個'指針

我得到這個錯誤void traverse功能,我已經使用insert功能插入地圖。

struct node { 
    int weight; 
    unsigned char value; 
    const node *child0; 
    const node *child1; 
    map<unsigned char, string> huffmanTable; 

    node(unsigned char c = 0, int i = -1) { 
     value = c; 
     weight = i; 
     child0 = 0; 
     child1 = 0; 
    } 

    node(const node* c0, const node *c1) { 
     value = 0; 
     weight = c0->weight + c1->weight; 
     child0 = c0; 
     child1 = c1; 
    } 

    bool operator<(const node &a) const { 
     return weight >a.weight; 
    } 

    void traverse(ostream& o,string code="") const { 

    if (child0) { 
     child0->traverse(o, code + '0'); 
     child1->traverse(o, code + '1'); 
    } else { 
     o<<value<<"\t"; 
     cout <<" " <<value <<" "; 

     o<<weight<<"\t"; 
     cout <<weight; 
     o<<code<<endl; 

     cout <<"  " <<code <<endl; 
     huffmanTable.insert(pair<unsigned char, std::string>(value,code)); 

    } 
} 

}; 

回答

3

您嘗試添加到地圖huffmanTable從一個const函數。一個const成員函數不允許修改這個對象的。你的選擇是

  1. 可以使地圖可變

    可變地圖huffmanTable;

  1. 從橫向運動的功能取出常量

    無效移動(ostream的& O,串碼= 「」){

+0

你救了我很多時間。謝謝 – Rajul 2012-07-22 16:17:32

+0

@Rajul我希望你沒有讓變量'mutable'。我傾向於低估這個答案只是爲了表明這一點。 – 2012-07-22 16:22:04

+0

@Luchian我已經從功能中刪除了const – Rajul 2012-07-22 16:41:12

相關問題