免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
123下一页
最近访问板块 发新帖
查看: 10801 | 回复: 20

[C] [问题解决]gcc编译报错----syntax error before "struct" [复制链接]

论坛徽章:
0
发表于 2009-01-14 16:38 |显示全部楼层
[fly]问题解决, 因为所包含的头文件没有定义一个ElemType类型, 所以一直报错.[/fly]




这是Status.h
#include <stdlib.h>
#include <stdio.h>
/* function return type code */
#define TURE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
/* Status is return type */
typedef int Status



这是Sqlist.c
#include "Status.h"
#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10

struct SqList {
    ElemType *elem;
    int length;
    int listsize;
};
typedef struct SqList SqList;

/* ALG 2.3 */
Status InitList_Sq(SqList *L) {
    L->elem = (ElemType *)malloc(LIST_INIT_SIZE * sizeof(ElemType));
    if(!L->elem)
        exit(OVERFLOW);
    L->length = 0;
    L->listsize = LIST_INIT_SIZE;
    return OK;
}/* InitList_Sq */

/* ALG 2.4 */
Status ListInsert_Sq(Sqlist *L, int i, ElemType e) {
if(i<0 || i>(L->length))
        return ERROR;

    if(L->length >= L->listsize) {
        ElemType *newbase;
        newbase = (ElemType *)realloc(L->elem,
                        (L->listsize + LISTINCREMENT) * sizeof(ElemType));
        if(!newbase)
            exit(OVERFLOW);
        L->elem = newbase;
        L->listsize += LISTINCREMENT;
    }

    ElemType *p, *q;
    q = L->elem + i;
    for(p = L->elem + l->length; p>q; --p)
        *p = *(p - 1);
    *q = e;
    ++L.length;
    return OK;
}/* ListInsert_Sq */

/* ALG 2.5 */
Status ListDelete_Sq(Sqlist *L, int i, ElemType *e) {
    if((i<0) || (i>(L->lenth - 1)))
        return ERROR;
    ElemType *p, *q;
    p = L->elm + i;
    *e = *p;
    for(q = L->elem + L.length - 1; q>p; --q)
        *(q - 1) = *q;
    --(L->length);
    return OK;
}/* ListDelete_Sq */




这是编译的错误信息
[mike@localhost datastc]$ gcc -Wall -c Sqlist.c
Sqlist.c:5: syntax error before "struct"
Sqlist.c:6: parse error before "ElemType"
Sqlist.c:6: warning: no semicolon at end of struct or union
Sqlist.c:9: parse error before '}' token
Sqlist.c:13: parse error before "InitList_Sq"
Sqlist.c:13: warning: return type defaults to `int'
Sqlist.c: In function `InitList_Sq':
Sqlist.c:14: dereferencing pointer to incomplete type
Sqlist.c:14: `ElemType' undeclared (first use in this function)
Sqlist.c:14: (Each undeclared identifier is reported only once
Sqlist.c:14: for each function it appears in.)
Sqlist.c:14: parse error before ')' token
Sqlist.c:15: dereferencing pointer to incomplete type
Sqlist.c:17: dereferencing pointer to incomplete type
Sqlist.c:18: dereferencing pointer to incomplete type
Sqlist.c: At top level:
Sqlist.c:23: parse error before "ListInsert_Sq"
Sqlist.c:23: parse error before '*' token
Sqlist.c:23: warning: return type defaults to `int'
Sqlist.c: In function `ListInsert_Sq':
Sqlist.c:24: `i' undeclared (first use in this function)
Sqlist.c:24: `L' undeclared (first use in this function)
Sqlist.c:28: `ElemType' undeclared (first use in this function)
Sqlist.c:28: `newbase' undeclared (first use in this function)
Sqlist.c:29: parse error before ')' token
Sqlist.c:37: `p' undeclared (first use in this function)
Sqlist.c:37: `q' undeclared (first use in this function)
Sqlist.c:37: warning: left-hand operand of comma expression has no effect
Sqlist.c:39: `l' undeclared (first use in this function)
Sqlist.c:41: `e' undeclared (first use in this function)
Sqlist.c: At top level:
Sqlist.c:47: parse error before "ListDelete_Sq"
Sqlist.c:47: parse error before '*' token
Sqlist.c:47: warning: return type defaults to `int'
Sqlist.c: In function `ListDelete_Sq':
Sqlist.c:48: `i' undeclared (first use in this function)
Sqlist.c:48: `L' undeclared (first use in this function)
Sqlist.c:50: `ElemType' undeclared (first use in this function)
Sqlist.c:50: `p' undeclared (first use in this function)
Sqlist.c:50: `q' undeclared (first use in this function)
Sqlist.c:50: warning: left-hand operand of comma expression has no effect
Sqlist.c:52: `e' undeclared (first use in this function)

[ 本帖最后由 argstormsky 于 2009-1-15 22:43 编辑 ]

论坛徽章:
0
发表于 2009-01-14 16:42 |显示全部楼层
这个文件不全吧

cat -n Sqlist.h 贴出来看看

[ 本帖最后由 smallstar001 于 2009-1-14 16:44 编辑 ]

论坛徽章:
0
发表于 2009-01-14 17:06 |显示全部楼层

回复 #2 smallstar001 的帖子

就是以数据结构头文件

/*#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10*/

struct SqList {
&nbsp;&nbsp;&nbsp;&nbsp;ElemType *elem;
&nbsp;&nbsp;&nbsp;&nbsp;int length;
&nbsp;&nbsp;&nbsp;&nbsp;int listsize;
};
typedef struct SqList Sqlist;

/* ALG 2.3 */
Status InitList_Sq(SqList *L) {
&nbsp;&nbsp;&nbsp;&nbsp;L->elem = (ElemType *)malloc(LIST_INIT_SIZE * sizeof(ElemType));
&nbsp;&nbsp;&nbsp;&nbsp;if(!L->elem)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exit(OVERFLOW);
&nbsp;&nbsp;&nbsp;&nbsp;L->length = 0;
&nbsp;&nbsp;&nbsp;&nbsp;L->listsize = LIST_INIT_SIZE;
&nbsp;&nbsp;&nbsp;&nbsp;return OK;
}/* InitList_Sq */


/* ALG 2.4 */
Status ListInsert_Sq(Sqlist *L, int i, ElemType e) {
&nbsp;&nbsp;&nbsp;&nbsp;if(i<0 || i>(L->length))
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return ERROR;

&nbsp;&nbsp;&nbsp;&nbsp;if(L->length >= L->listsize) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ElemType *newbase;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;newbase = (ElemType *)realloc(L->elem,
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(L->listsize + LISTINCREMENT) * sizeof(ElemType));
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(!newbase)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exit(OVERFLOW);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;L->elem = newbase;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;L->listsize += LISTINCREMENT;
&nbsp;&nbsp;&nbsp;&nbsp;}

&nbsp;&nbsp;&nbsp;&nbsp;ElemType *p, *q;
&nbsp;&nbsp;&nbsp;&nbsp;q = L->elem + i;
&nbsp;&nbsp;&nbsp;&nbsp;for(p = L->elem + l->length; p>q; --p)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*p = *(p - 1);
&nbsp;&nbsp;&nbsp;&nbsp;*q = e;
++L.length;
&nbsp;&nbsp;&nbsp;&nbsp;return OK;
}/* ListInsert_Sq */

/* ALG 2.5 */
Status ListDelete_Sq(Sqlist *L, int i, ElemType *e) {
&nbsp;&nbsp;&nbsp;&nbsp;if((i<0) || (i>(L->lenth - 1)))
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return ERROR;
&nbsp;&nbsp;&nbsp;&nbsp;ElemType *p, *q;
&nbsp;&nbsp;&nbsp;&nbsp;p = L->elm + i;
&nbsp;&nbsp;&nbsp;&nbsp;*e = *p;
&nbsp;&nbsp;&nbsp;&nbsp;for(q = L->elem + L.length - 1; q>p; --q)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*(q - 1) = *q;
&nbsp;&nbsp;&nbsp;&nbsp;--(L->length);
&nbsp;&nbsp;&nbsp;&nbsp;return OK;
}/* ListDelete_Sq */


[ 本帖最后由 argstormsky 于 2009-1-14 17:08 编辑 ]

论坛徽章:
0
发表于 2009-01-14 17:23 |显示全部楼层
第10行的参数

SqList *L 错了
---->
Sqlist *L

论坛徽章:
0
发表于 2009-01-14 17:25 |显示全部楼层
关键不在这里, gcc说我第一句就错了, 我囧....

论坛徽章:
0
发表于 2009-01-14 17:39 |显示全部楼层
原帖由 argstormsky 于 2009-1-14 17:06 发表
就是以数据结构头文件


既然是头文件,怎么里面还定义了函数?
这种写法好像不允许吧
最起码不太规范

[ 本帖最后由 zhuhefang2006 于 2009-1-14 17:41 编辑 ]

论坛徽章:
0
发表于 2009-01-14 17:48 |显示全部楼层
原帖由 argstormsky 于 2009-1-14 17:25 发表
关键不在这里, gcc说我第一句就错了, 我囧....

我测试了一下,没有在定义struct SqList处报错,是不是文件里包含里一些隐藏字符
代码是你自己输进去的,还是你从哪里粘帖过去的?如果是你从别的地方粘帖过来的,很有可能包含一些隐藏字符,检查一下呢

论坛徽章:
0
发表于 2009-01-14 17:50 |显示全部楼层
感觉上
/*#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10*/
struct SqList {
附近有什么怨灵

比如
标点符号不对,有什么奇怪的字符啊之类

论坛徽章:
0
发表于 2009-01-14 18:50 |显示全部楼层
都是我自己用vim打得.

论坛徽章:
0
发表于 2009-01-14 18:54 |显示全部楼层

回复 #8 waternie 的帖子



[ 本帖最后由 samon_fu 于 2009-1-14 20:22 编辑 ]
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

DTCC2020中国数据库技术大会

【架构革新 高效可控】2020年12月21日-23日第十一届中国数据库技术大会将在北京隆重召开。

大会设置2大主会场,20+技术专场,将邀请超百位行业专家,重点围绕数据架构、AI与大数据、传统企业数据库实践和国产开源数据库等内容展开分享和探讨,为广大数据领域从业人士提供一场年度盛会和交流平台。

http://dtcc.it168.com


大会官网>>
  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP