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

如果您需要更多信息,请告诉我,谢谢!

4

1 回答 1

2

您的头文件以循环方式相互包含。这就是你的错误的原因。

切勿尝试循环包含。它一无所获,只能导致错误。将您的标题重新设计为“分层”层次结构:较高级别的标题包含较低级别的标题,但反之则不然。

在您的情况下,可以通过以下两种方式之一消除循环包含:

  1. 停止包含WordList.hFileOccur.h. 而是提供前向声明

    typedef struct word word;
    

    FileOccur.h.

  2. 停止包含FileOccur.hWordList.h. 而是提供前向声明

    typedef struct FileOccur FileOccur; 
    

    WordList.h.

哪种方法更好取决于您认为哪个头文件是更高级别的。

另外,请参阅此处 避免头文件的循环依赖

于 2013-11-04T18:08:02.557 回答