Chinaunix

标题: [问题解决]gcc编译报错----syntax error before "struct" [打印本页]

作者: argstormsky    时间: 2009-01-14 16:38
标题: [问题解决]gcc编译报错----syntax error before "struct"
[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 编辑 ]
作者: smallstar001    时间: 2009-01-14 16:42
这个文件不全吧

cat -n Sqlist.h 贴出来看看

[ 本帖最后由 smallstar001 于 2009-1-14 16:44 编辑 ]
作者: argstormsky    时间: 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 编辑 ]
作者: smallstar001    时间: 2009-01-14 17:23
第10行的参数

SqList *L 错了
---->
Sqlist *L
作者: argstormsky    时间: 2009-01-14 17:25
关键不在这里, gcc说我第一句就错了, 我囧....
作者: zhuhefang2006    时间: 2009-01-14 17:39
原帖由 argstormsky 于 2009-1-14 17:06 发表
就是以数据结构头文件


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

[ 本帖最后由 zhuhefang2006 于 2009-1-14 17:41 编辑 ]
作者: zhuhefang2006    时间: 2009-01-14 17:48
原帖由 argstormsky 于 2009-1-14 17:25 发表
关键不在这里, gcc说我第一句就错了, 我囧....

我测试了一下,没有在定义struct SqList处报错,是不是文件里包含里一些隐藏字符
代码是你自己输进去的,还是你从哪里粘帖过去的?如果是你从别的地方粘帖过来的,很有可能包含一些隐藏字符,检查一下呢
作者: waternie    时间: 2009-01-14 17:50
感觉上
/*#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10*/
struct SqList {
附近有什么怨灵

比如
标点符号不对,有什么奇怪的字符啊之类
作者: argstormsky    时间: 2009-01-14 18:50
都是我自己用vim打得.
作者: samon_fu    时间: 2009-01-14 18:54
标题: 回复 #8 waternie 的帖子


[ 本帖最后由 samon_fu 于 2009-1-14 20:22 编辑 ]
作者: 自由建客    时间: 2009-01-14 22:50
typedef int Status
少了分号
作者: 冻惨鸟    时间: 2009-01-14 23:02
这种错误 我昨天才犯了
作者: 冻惨鸟    时间: 2009-01-14 23:04
补充一句,认真分析编译器报的错误是很重要的
作者: zhuhefang2006    时间: 2009-01-14 23:12
楼主把自己的问题帖都改了
还以为真的什么奇异的事情发生了呢,关注了这么久

[ 本帖最后由 zhuhefang2006 于 2009-1-14 23:29 编辑 ]
作者: smallstar001    时间: 2009-01-15 08:57
问题解决了没?
作者: xiexiecn    时间: 2009-01-15 09:09
标题: 回复 #15 smallstar001 的帖子
找到问题不是关键,怎么找才重要。这种情况,我多半是通过用#if 0 ...  #endif大段注释代码来查找,很容易就找到了。
作者: eveson    时间: 2009-01-15 12:54
ElemType 如何定义的?
作者: snick    时间: 2009-01-15 13:18
"typedef int Status"  ??
作者: JohnBull    时间: 2009-01-15 17:49
.h后面少分号
作者: yangsf5    时间: 2009-01-15 19:26

LZ啊,建议你最后一句话写到最前吧,或者在帖子标题上加个“已解决”之类的。。
同志们还以为你没解决呢。。
作者: eveson    时间: 2009-01-16 12:29
原帖由 eveson 于 2009-1-15 12:54 发表
ElemType 如何定义的?


我还以为你没有把文件发全那,原来还真是这个原因




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2