2013-11-04 45 views
2

我想在C多在編譯「錯誤:之前預期‘)’‘*’令牌

當我用下面的參數編譯編程....

gcc -D_BSD_SOURCE -Wall -ansi -pedantic -g tokenizer.c FileOccur.c WordList.c wordstat.c indexer.c -o indexer 

我得到這個從終端作爲響應:

In file included from ../Headers/WordList.h:11, 
       from FileOccur.c:12: 
../Headers/FileOccur.h:18: error: expected ‘)’ before ‘*’ token 
../Headers/FileOccur.h:20: error: expected ‘)’ before ‘*’ token 
In file included from ../Headers/WordList.h:11, 
       from WordList.c:11: 
../Headers/FileOccur.h:18: error: expected ‘)’ before ‘*’ token 
../Headers/FileOccur.h:20: error: expected ‘)’ before ‘*’ token 
WordList.c: In function ‘insert_List’: 
WordList.c:155: warning: implicit declaration of function ‘insert_FileOccur’ 
In file included from ../Headers/WordList.h:11, 
       from wordstat.c:11: 
../Headers/FileOccur.h:18: error: expected ‘)’ before ‘*’ token 
../Headers/FileOccur.h:20: error: expected ‘)’ before ‘*’ token 
In file included from ../Headers/WordList.h:11, 
       from indexer.c:11: 
../Headers/FileOccur.h:18: error: expected ‘)’ before ‘*’ token 
../Headers/FileOccur.h:20: error: expected ‘)’ before ‘*’ token 

這裏是我的FileOccur.h文件:

/* 
* FileOccur.h 
* Includes list of all functions declared in FileOccur.c so that other source files 
* can use them. Also defines the fields for the structs used for FileOccur.c 
*/ 
#ifndef FILEOCCUR_H 
#define FILEOCCUR_H 

#include "../Headers/WordList.h" 
#include <stdio.h> 
#include <stdlib.h> 

typedef struct FileOccur FileOccur; 


FileOccur * create_FileOccur(char * fileName); 
void fprint_FileOccur(FILE * indexFile, FileOccur * currFileOccur); 
void free_Occur(FileOccur * curr); 
int insert_FileOccur(word * lCurr,char * fileName); 
void print_FileOccur(FileOccur * currFileOccur); 
int sort_TotalOccur(word * lCurr, FileOccur * prevFile, FileOccur * currFile); 

struct FileOccur{ /* 
        * the FileOccur struct is used as a node to keep information about how many times an 
        * an item was found in a file, the file's name and the next file that has the same 
        * item in it, in this case a word 
        * 
        */ 

    char * fileName; 
    int occur; 
    FileOccur * next; 
}; 

#endif 

,這裏是我的WordList.h文件:

/* 
* WordList.h 
* Includes list of all functions declared in WordList.c so that other source files 
* can use them. Also defines the fields for the structs used for WordList.c 
*/ 
#ifndef WORDLIST_H 
#define WORDLIST_H  

#include <stdio.h> 
#include <stdlib.h> 
#include "FileOccur.h" 


typedef struct word word; 
typedef struct wordList list; 

word * create_Word(char * spell, char * fileName); 
void fprint_List(FILE * indexFile, list * words); 
void free_List(list * words); 
int insert_List(char * currString, list * words, char * fileName); 
void print_List(list * words); 

struct word{ /* the word struct used as a node to keep important information about each word including 
       * its occurences in the various files, the next word in the list, and the spelling of the 
       * current word. 
       */ 
    char * spell; 
    struct word * next; 
    FileOccur * totalOccur; 
}; 


struct wordList{ /* my linked list that simply has a pointer to the first node*/ 
    word * start; 

}; 


#endif 

請讓我知道如果你需要任何更多信息,謝謝!

回答

2

您的頭文件以循環方式相互包含。這是你錯誤的原因。

永遠不要嘗試循環包含。它什麼都不做,只會導致錯誤。將您的標題重新設計爲「分層」層次結構:高層標題包含較低層標題,但不是相反。

在你的情況下,圓形夾雜可以通過以下兩種方式之一被淘汰:

  1. 停止包括WordList.hFileOccur.h。而是提供正向聲明

    typedef struct word word; 
    

    in FileOccur.h

  2. 停止包括FileOccur.hWordList.h。相反,在WordList.h提供向前聲明

    typedef struct FileOccur FileOccur; 
    

哪種方法更好取決於哪個頭文件被認爲是更高級的頭文件。

而且,在這裏看到 Avoiding Circular Dependencies of header files

+0

感謝你的解釋和解決! –

相關問題