在编译“错误:预期”)'之前的'*'令牌时出现多个相同的错误 [英] Multiple of same error while compiling "error: expected ')' before '*' token

查看:277
本文介绍了在编译“错误:预期”)'之前的'*'令牌时出现多个相同的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在C中编程。



使用以下参数编译时

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

我从终端收到这个回应:

 从../Headers/WordList.h:11,
文件包括从FileOccur.c:12:
../Headers/FileOccur.h:18:错误:预期')'之前'*'token
../Headers/FileOccur.h:20:error:expected')'before'*'token
在包含在../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:在函数'insert_List'中:
WordList.c:155:warning:隐含声明函数'insert_FileOccur'
文件包括从../Headers/WordList.h:11,
从wordstat.c:11:
../Headers/FileOccur.h:18:错误:预期')'之前'*' token
../Headers/FileOccur.h:20:error:expected')'before'*'token
在从../Headers/WordList.h:11,
from indexer.c:11:
../Headers/FileOccur.h:18:error:expected')'before'*'token
../Headers/FileOccur.h:20:error:预期')'before'*'标记

这是我的FileOccur.h文件:

  / * 
* FileOccur.h
*包括FileOccur.c中声明的所有函数的列表,
*可以使用它们。还定义用于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 {/ *
* FileOccur结构用作一个节点,以保存有关在文件中找到
*项目的次数的信息,文件的名称和在其中具有相同
*项的下一个文件,在这种情况下是字
*
* /

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

#endif

这里是我的WordList.h文件:

  / * 
* WordList.h
*包括在WordList.c中声明的所有函数的列表,源文件
*可以使用它们。还定义用于WordList.c的结构体的字段
* /
#ifndef WORDLIST_H
#define WORDLIST_H

#include< stdio.h>
#include< stdlib.h>
#includeFileOccur.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,包括
*它在各种文件中的出现,列表中的下一个单词以及拼写的
*当前词。
* /
char * spell;
struct word * next;
FileOccur * totalOccur;
};


struct wordList {/ *我的链表只有一个指向第一个节点的指针* /
word * start;

};


#endif

请让我知道如果你需要任何更多信息,谢谢!

解决方案

您的头文件以圆形方式包含在一起。这是您的错误的原因。



不要尝试循环包含。它没有什么,只能导致错误。



在你的情况下,循环包含可以是一个分层的头文件,以下两种方式之一消除:


  1. 停止包括 WordList.h FileOccur.h 。而是提供转发声明

      typedef struct word word; 

    FileOccur.h

    。而是提供向前声明

      typedef struct FileOccur FileOccur; 

    WordList.h


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



另请参阅
避免循环依赖性头文件


I am trying to program in C.

When I compile with the following arguments....

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

I get this from terminal as a response:

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

Here is my FileOccur.h file:

/*
 * 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 

and here is my WordList.h file:

/*
 * 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

Please let me know if you need any more information, thank you!

解决方案

Your header files include each other in circular fashion. This is the reason for your error.

Never attempt circular inclusion. It achieves nothing and can only lead to errors. Redesign your headers into a "stratified" hierarchy: higher-level headers include lower-level headers, but not the other way around.

In your case the circular inclusion can be eliminated in one of two ways:

  1. Stop including WordList.h into FileOccur.h. Instead provide forward declaration

    typedef struct word word;
    

    in FileOccur.h.

  2. Stop including FileOccur.h into WordList.h. Instead provide forward declaration

    typedef struct FileOccur FileOccur; 
    

    in WordList.h.

Which approach is better depends on which header file you consider to be a higher-level one.

Also, see here Avoiding Circular Dependencies of header files

这篇关于在编译“错误:预期”)'之前的'*'令牌时出现多个相同的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆