注册 登录
编程论坛 数据结构与算法

有关哈夫曼树初始化问题

桑榆 发布于 2015-06-29 23:50, 2294 次点击
for(i=1;i<=n;i++)    //对叶子节点初始化。
    {
        printf("输入第%d个字符基本信息:",i);
        scanf("%c,%d",&HT[i].data,&HT[i].weight);  第4行
        HT[i].parent=0;                            第5行
        HT[i].lchild=0;                            第6行
        HT[i].rchild=0;                            第7行
    }

在第4行到第7行依次出现了一下一样的问题,求解答???
 error C2228: left of '.data' must have class/struct/union type
 error C2228: left of '.weight' must have class/struct/union type
 error C2228: left of '.parent' must have class/struct/union type
 error C2228: left of '.lchild' must have class/struct/union type
 error C2228: left of '.rchild' must have class/struct/union type
6 回复
#2
林月儿2015-06-30 08:39
HT[i].parent=NULL;                            第5行
        HT[i].lchild=NULL;                            第6行
        HT[i].rchild=NULL;                            第7行
    }
#3
桑榆2015-06-30 09:34
啊!谢谢,不过我这个程序又出现了新的问题。我把生成的哈弗曼编码存入一位数组cd[]中,然后利用函数Strcpy()将一维数组复制给二维数组tco[][]的第i行,即tco[i][],请问调用函数的时候可不可以这样:Strcpy(tco[i][],cd);
但是就出现这样的错误了,这是怎么回事啊???
Status Strcpy(char tco[i][CODINGMAX],char cd[CODINGMAX])    //将求得的编码放入二维数组tco[][]中。改:cd。
{
    int j;
    for(j=1;cd[j]!='\0';j++)
        tco[i][j]=cd[j];
    return OK;
}
f:\哈夫曼编码器译码器\hfmcoding\string.h(6) : error C2065: 'i' : undeclared identifier
f:\哈夫曼编码器译码器\hfmcoding\string.h(6) : error C2057: expected constant expression
f:\哈夫曼编码器译码器\hfmcoding\string.h(6) : error C2466: cannot allocate an array of constant size 0
string.cpp
f:\哈夫曼编码器译码器\hfmcoding\string.h(6) : error C2065: 'i' : undeclared identifier
f:\哈夫曼编码器译码器\hfmcoding\string.h(6) : error C2057: expected constant expression
f:\哈夫曼编码器译码器\hfmcoding\string.h(6) : error C2466: cannot allocate an array of constant size 0
f:\哈夫曼编码器译码器\hfmcoding\string.cpp(31) : error C2057: expected constant expression
f:\哈夫曼编码器译码器\hfmcoding\string.cpp(31) : error C2466: cannot allocate an array of constant size 0
#4
林月儿2015-06-30 11:55
f:\哈夫曼编码器译码器\hfmcoding\string.h(6) : error C2065: 'i' : undeclared identifier
变量i没有声明
 for(j=1;cd[j]!='\0';j++)
        tco[i][j]=cd[j];
    return OK;
f:\哈夫曼编码器译码器\hfmcoding\string.cpp(31) : error C2057: expected constant expression
f:\哈夫曼编码器译码器\hfmcoding\string.cpp(31) : error C2466: cannot allocate an array of constant size 0
31行的代码看不到,不能解释
#5
桑榆2015-06-30 12:27
它是这样的:
13行:    Status Strcpy(char tco[i][CODINGMAX],char cd)    //将求得的编码放入二维数组tco[][]中。#define CODINGMAX 30
          {   
              int j;
16行:        for(j=1;cd[j]!='\0';j++)
                  tco[i][j]=cd[j];
              return OK;
          }
f:\哈夫曼编码器译码器\hfmcoding\string.cpp(13) : error C2057: expected constant expression
f:\哈夫曼编码器译码器\hfmcoding\string.cpp(13) : error C2466: cannot allocate an array of constant size 0
f:\哈夫曼编码器译码器\hfmcoding\string.cpp(16) : error C2109: subscript requires array or pointer type
f:\哈夫曼编码器译码器\hfmcoding\string.cpp(17) : error C2109: subscript requires array or pointer type
main.cpp

在调用这个函数的时候是这样的:
134行:  Strcpy(tco[i][],cd);    //将求得的编码放入二维数组tco[i][]中。
f:\哈夫曼编码器译码器\hfmcoding\coding.cpp(134) : error C2059: syntax error : ']'

在定义的时候是这样的:
30行:  Status Strcpy(char tco[i][CODINGMAX],char cd);    //将求得的编码放入二维数组tco[][]中。
f:\哈夫曼编码器译码器\hfmcoding\string.h(30) : error C2065: 'i' : undeclared identifier
f:\哈夫曼编码器译码器\hfmcoding\string.h(30) : error C2057: expected constant expression
f:\哈夫曼编码器译码器\hfmcoding\string.h(30) : error C2466: cannot allocate an array of constant size 0

嗯!!总的来说应该是二维数组调用时候参数的问题!!我想直接把二维数组tco[][]的第i行传入函数。但是好像不太会
#6
林月儿2015-06-30 12:31
回复 5楼 桑榆
一点点的代码,无能为力。楼主好自为之
#7
桑榆2015-06-30 20:29
,还是谢谢版主了!
1