免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 1813 | 回复: 15
打印 上一主题 下一主题

初学者问题,是在不知道错再哪里了 [复制链接]

论坛徽章:
0
1 [报告]
发表于 2006-04-17 08:45 |只看该作者
提前引用了~

可以这样定义:
typedef struct   _NODE_{
        char *productName;
        char *productCata;
        int *productNum;
        int *productPreis;
        struct  _NODE_* left;
        struct  _NODE_* right;
} Node;

论坛徽章:
0
2 [报告]
发表于 2006-04-17 09:15 |只看该作者
原帖由 peng971237 于 2006-4-17 08:45 发表
提前引用了~

可以这样定义:
typedef struct   _NODE_{
        char *productName;
        char *productCata;
        int *productNum;
        int *productPreis;
        struct  _NODE_* left; ...

这一段替换好了,
在list_add里面也改成_NODE_ *root了,重新编译出现这样的错误:

binaryT.c:30: error: parse error before '*' token
binaryT.c: In function 'list_add':
binaryT.c:32: error: dereferencing pointer to incomplete type
binaryT.c:32: error: 'name' undeclared (first use in this function)
binaryT.c:32: error: (Each undeclared identifier is reported only once
binaryT.c:32: error: for each function it appears in.)
binaryT.c:33: error: dereferencing pointer to incomplete type
binaryT.c:33: error: 'cata' undeclared (first use in this function)
binaryT.c:34: error: dereferencing pointer to incomplete type
binaryT.c:34: error: 'num' undeclared (first use in this function)
binaryT.c:35: error: dereferencing pointer to incomplete type
binaryT.c:35: error: 'preis' undeclared (first use in this function)

[ 本帖最后由 stellit 于 2006-4-17 09:18 编辑 ]

论坛徽章:
1
荣誉版主
日期:2011-11-23 16:44:17
3 [报告]
发表于 2006-04-17 09:21 |只看该作者
原帖由 peng971237 于 2006-4-17 08:45 发表
提前引用了~

可以这样定义:
typedef struct   _NODE_{
        char *productName;
        char *productCata;
        int *productNum;
        int *productPreis;
        struct  _NODE_* left; ...

你把你修改后的完整代码贴上来。

论坛徽章:
0
4 [报告]
发表于 2006-04-17 09:24 |只看该作者
原帖由 stellit 于 2006-4-17 09:15 发表

这一段替换好了,
在list_add里面也改成_NODE_ *root了,重新编译出现这样的错误:


可以肯定的说,变量未定义,如果是对结构的引用,则肯定写法不正确。
至于第一个error,不是很肯定

论坛徽章:
0
5 [报告]
发表于 2006-04-17 09:26 |只看该作者
30 行就是那个list_add那一行,其实还想写一个这样的 struct :
  1. typedef struct {
  2. char *productName;
  3. char *productCata;
  4. int  productNum;
  5. int  productPreis;
  6. } Data;
复制代码

把这四个提出来Node写成这样:
  1. typedef struct {
  2.     struct Data *data
  3.     struct Node *left;
  4.     struct Node *right;
  5. } Node;
复制代码

但是不知道怎么写,在网上找过,可是找不到。就像java一样,对象里面的对象变量

[ 本帖最后由 stellit 于 2006-4-17 09:33 编辑 ]

论坛徽章:
0
6 [报告]
发表于 2006-04-17 09:30 |只看该作者
》》 struct Node *node = (struct Node*)malloc(sizeof(Node));
modify to: Node *node = (Node*)malloc(sizeof(Node));
"Node" is only a alias.

论坛徽章:
0
7 [报告]
发表于 2006-04-17 09:36 |只看该作者
  1. 》》 struct Node *node = (struct Node*)malloc(sizeof(Node));
  2. modify to: Node *node = (Node*)malloc(sizeof(Node));
  3. "Node" is only a alias.
复制代码

Modified, but got the same error as the first one.

论坛徽章:
1
荣誉版主
日期:2011-11-23 16:44:17
8 [报告]
发表于 2006-04-17 09:36 |只看该作者
typedef struct {
    struct Data *data
    struct Node *left;
    struct Node *right;
} Node;

你应该把里面的struct去掉。
你要明白typedef的含义。

论坛徽章:
0
9 [报告]
发表于 2006-04-17 09:37 |只看该作者
原帖由 stellit 于 2006-4-17 09:36 发表
  1. 》》 struct Node *node = (struct Node*)malloc(sizeof(Node));
  2. modify to: Node *node = (Node*)malloc(sizeof(Node));
  3. "Node" is only a alias.
复制代码

Modified, but got the same error  ...

把完整代码贴出来吧

论坛徽章:
0
10 [报告]
发表于 2006-04-17 09:39 |只看该作者
  1. #include<string.h>
  2. #include<stdio.h>
  3. #include<stdlib.h>



  4. //typedef struct {
  5. //  char *productName;
  6. //  char *productCata;
  7. //  int  productNum;
  8. //  int  productPreis;
  9. //
  10. //} Data;


  11. typedef struct  {
  12.     char *productName;
  13.     char *productCata;
  14.     int *productNum;
  15.     int *productPreis;
  16.     struct Node *left;
  17.     struct Node *right;
  18. } Node;

  19. void list_add(Node *root, char *name, char *cata, int num, int preis){
  20.     struct Node *node = (Node*)malloc(sizeof(Node));
  21.     node->productName = (char *) malloc(strlen(name) + 1);
  22.     node->productCata = (char *) malloc(strlen(cata) + 1);
  23.     node->productNum = (int *)malloc(sizeof(num));
  24.     node->productPreis=(int *)malloc(sizeof(preis));
  25. }
复制代码


binaryT.c: In function 'list_add':
binaryT.c:26: warning: initialization from incompatible pointer type
binaryT.c:27: error: dereferencing pointer to incomplete type
binaryT.c:28: error: dereferencing pointer to incomplete type
binaryT.c:29: error: dereferencing pointer to incomplete type
binaryT.c:30: error: dereferencing pointer to incomplete type

刚接触C 不久,感觉C的指针比面向对象的思想还难
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP