gpt4 book ai didi

c++ - 错误 : error C2663: 'std::_Tree<_Traits>::insert' : 2 overloads have no legal conversion for 'this' pointer

转载 作者:搜寻专家 更新时间:2023-10-31 01:53:05 26 4
gpt4 key购买 nike

我遇到了以上错误,如果有人有任何想法请帮助我。

我在 void traverse 函数中遇到此错误,我使用 insert 函数插入 map 。

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

}
}

};

最佳答案

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

  1. 您可以使 map 可变

    可变映射 huffmanTable;

  1. 从遍历函数中移除const

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

关于c++ - 错误 : error C2663: 'std::_Tree<_Traits>::insert' : 2 overloads have no legal conversion for 'this' pointer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11601618/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com