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));

    }
}

};
4

1 回答 1

4

您正在尝试从 const 函数添加到地图huffmanTable 。不允许 const 成员函数修改this对象。你的选择是

  1. 您可以使地图可变

    可变映射霍夫曼表;

或者

  1. 从遍历函数中删除 const

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

于 2012-07-22T15:43:54.843 回答