sirxiangsir 2015-11-17 09:20 采纳率: 3.7%
浏览 1514

下面的错误怎么改啊,请大家帮帮我,我刚开始写,写不好啊。耐心看一下,谢谢!!

 #include <iostream>
#include <string>
#define MAX 100
using namespace std;

class HTNode
{
    public:
    int weight;
    string data;
    int f;
    HTNode* father;
    HTNode* lchild;
    HTNode* rchild;
    string code;
    HTNode()
    {
        weight=0;
        data='0';
        code='0';
        f=0;
        father=NULL;
        lchild=NULL;
        rchild=NULL;
    }
        void SetChild (HTNode* lchild, HTNode* rchild) {
        this->lchild = lchild;
        this->rchild = rchild;
    }
    void SetF(int f) {
        this->f = f;
    }
    void SetFather(HTNode* father) {
        this->father = father;
    }
};


void SelectMin(HTNode*ht[], int*a1,int*a2)
{
    int min1=0;
    int min2=0;
    for(int i=0;i<2*MAX-1;i++)
    {
        if(ht[min1]->weight>ht[i]->weight)
        min1=i;     
    }
    min2=min1+1;
    for(int i=0;i<2*MAX-1;i++)
    {
        if(ht[min2]->weight>ht[i]->weight)
        min2=i;
    }
    *a1=min1;
    *a2=min2;
}

void Printtree(HTNode* root, int n) {

    if (root==NULL) 
    return;
    for (int i=0;i<n;i++) {
        cout<<" ";
    }
    if (root->f==1) {
        cout<<"|_";
    } else {
        cout<<"  ";
    }
    cout<<root->data<<endl;
    Printtree(root->lchild, n+2);
    Printtree(root->rchild, n+2);   
}



void Bianma(HTNode*ht[]) {

    HTNode* curr;
    string* code = new string[MAX];
    for (int i = 0; i < MAX ; i++) {
        code[i] = '2';
    }
    for (int i = 0; i < MAX; i++) {
        int j = 1;
        cout<<ht[i]->data<<"的哈弗曼编码为:";
        curr = ht[i];

        while (curr->f==1) {
            curr=curr->father;
            if (curr->f==1){
                code[j++]=curr->code;
            }
        }
        code[0] = ht[i]->code;
        string c[j];
        int count = 0;
        for (int l = j-1; l >=0; l--) {
            if (code[l]!= '2') {
                c[count++] = code[l];
            }
        }
        for (int x = 0; x < j; x++) {
            ht[i]->code += c[x];
        }
        cout<<ht[i]->code<<endl;
        cout<<endl;
    }

}


void FanYi(HTNode* ht[]) {

    string code;    
    string test = "";
    HTNode* curr;
    HTNode* root;
    cout<<"请输入编码:"<<endl;
    cin>>code;
    int n = code.length();
    string* cs = new string[n]; 
    code.copy(cs, n, 0);

    for (int i = 0; i < MAX*2-1; i++) {
        if (ht[i]->f == 0) {
            root = ht[i];
        }
    }
    curr = root;
    for (int i = 0; i < n; i++) {
        if (cs[i] == '0' && curr->lchild != NULL) {
            curr = curr->lchild;
            test += '0';
        } else if (cs[i] == '1') {
            curr = curr->rchild;
            test += '1';
        } 
        if (curr->lchild == NULL || curr->rchild == NULL) {
            curr = root;
            for (int i = 0; i < MAX; i++) {
                if (ht[i]->code == test) {
                    cout<<ht[i]->data;
                    test = "";
                }
            }
        }
    }
    cout<<endl; 
}

void GouJian(string* s, int* n) {

    int a1, a2, m;
    m = 2*MAX-1;
    HTNode* ht[2*MAX-1];

    for (int i=0;i<MAX;i++) {
        ht[i] = new HTNode(s[i], n[i]);
    }
    for (int i=MAX;i<2*MAX-1;i++) {
        ht[i] = new HTNode('0');
    }

    for (int i = 0; i < MAX-1; i++) {
        SelectMin(ht, &a1, &a2);
        ht[MAX+i]->weight = ht[a1]->weight + ht[a2]->weight;
        if (ht[a1]->weight <= ht[a2]->weight) {
            ht[MAX+i]->SetChild(ht[a1], ht[a2]);
            ht[a1]->code = '0';
            ht[a2]->code = '1';
        } else {
            ht[MAX+i]->SetChild(ht[a2], ht[a1]);
            ht[a1]->code = '1';
            ht[a2]->code = '0';
        }
        ht[a1]->SetF(1);
        ht[a2]->SetF(1);
        ht[a1]->SetFather(ht[MAX+i]);
        ht[a2]->SetFather(ht[MAX+i]);
        for (int i=0;i<2*MAX-1;i++) {
            cout<<ht[i]->weight<<"  ";
        }
        cout<<endl;
    }
    cout<<"您输入的哈夫曼树为:"<<endl<<endl;
    for (int i = 0;i<2*MAX-1;i++) {
        if (ht[i]->father == 0) {
            Printtree(ht[i], MAX);
            cout<<endl;
        }
    }

    cout<<"根据系统分析,有以下结论:"<<endl;
    Bianma(ht);
    cout<<endl;
    FanYi(ht);

}

错误信息
error: no matching function for call to std::basic_string<char, std::char_traits<char>, std::allocator<char> >::copy(std::string*&, int&, int)'
error: no match for 'operator!=' in '*((+(((unsigned int)l) * 4u)) + code) != '2'
error: no matching function for call to
HTNode::HTNode(char)'

  • 写回答

2条回答 默认 最新

  • yangbo50304 2015-11-17 09:43
    关注

    错误太多了,先把你的string的赋值和比较都写成双引号,别用单引号!

    评论

报告相同问题?

悬赏问题

  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划