gpt4 book ai didi

c - 在 ')' token 和 '*' 的冲突类型之前预期为 'function'

转载 作者:太空宇宙 更新时间:2023-11-04 08:35:49 25 4
gpt4 key购买 nike

大家好,我正在做一个类(class)项目,将文件中的单词添加到 trie 中,将其与另一个文件进行比较并显示某些信息。我似乎无法弄清楚为什么会出现这些错误。在我的 dictstat.c 文件中,这是我的代码。

  1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <ctype.h>
4 #include "prototypes.h"
5
6 typedef struct TrieNode
7 {
8 int isAWord ;
9 char letter;
10 int words;
11 struct TrieNode* children[26];
12 }Node;
13
14 typedef struct TrieADT
15 {
16 Node* root;
17 }MainTrie;
18
19 int main(int argc, char *argv[]){
20
21 MainTrie FirstTrie;
22 MainTrie *Trie;
23 Trie = &FirstTrie;
24
25 FILE *f, *f2;
26
27 if(argc != 3){
28 return 1;
29 }
30
31 f = fopen(argv[1], "r");
32 f2 = fopen(argv[2], "r");
33
34 if(f == NULL || f2 == NULL){
35 return 1;
36 }
37
38 makeTrie(Trie);
39
40 return 0;
41 }
42
43
44 Node *getNode(void) {
45
46 int i;
47 Node *newNode = NULL;
48 newNode = (Node *) malloc(sizeof(Node));//allocates space for new node
49 newNode->isAWord = 0;
50 newNode->letter = '/';
51
52 if( newNode ){
53
54 for(i = 0; i < 26; i++){
55
56 newNode->children[i] = NULL;//Sets elements in array equal to Null
57
58 }
59 return newNode;
60 }
61 }
62
63
64 void makeTrie(MainTrie *pTrie){
65
66 pTrie->root = getNode();
67 }
68
69 int addToTrie(char word[], int lengthOfWord, Node *TNode, int point){
70
71 if(lengthOfWord == 0){//if the length of the word is 0 stop the function
72
73 printf("No word.\n");
74 return 0;
75 }
76
77 if(point >= lengthOfWord){//if the pointer is beyond the end of a word
78
79 TNode->isAWord = 1;//then set the node to be the end of the word
80 return 1;
81 }
82
83 if(TNode == NULL){//if Node is null end function
84
85 printf("Pointer is null.\n");
86 return 0;
87 }
88
89 if( TNode->children[(word[point])-'a'] == NULL ){//if there is no neighbor with letter
90
91 Node *newNode;
92 newNode = getNode();//Makes new node
93 newNode->letter = word[point];//Puts letter in new node
94 TNode->children[(word[point])-'a'] = newNode;//puts new node into trie
95 point = point + 1;//increments down the tree
96 printf("adding\n");
97 return addToTrie(&word[point-1], lengthOfWord, TNode->children[(word[point]) - 'a'], point);//Calls function again
98 }
99
100 else{
101
102 printf("There is a neighbor.");//No new node to create
103 return addToTrie(&word[point-1], lengthOfWord, TNode->children[(word[point]) - 'a'], point);
104 }
106
107 }
108 }

这是我的 prototypes.h 文件。

  1 void readDict(FILE* dict_file);
2 void scanData(FILE* data_file);
3 void makeTrie(MainTrie *pTrie);
4 int addToTrie(char word[], int lengthOfWord, Node* TNode, int point);

当我尝试同时编译 .c 文件和 .h 文件时,我收到以下错误消息:

In file included from dictstat.c:4:
prototypes.h:3: error: expected ‘)’ before ‘*’ token
prototypes.h:4: error: expected declaration specifiers or ‘...’ before ‘Node’
dictstat.c:64: warning: conflicting types for ‘makeTrie’
dictstat.c:38: note: previous implicit declaration of ‘makeTrie’ was here
dictstat.c:69: error: conflicting types for ‘addToTrie’
prototypes.h:4: note: previous declaration of ‘addToTrie’ was here
prototypes.h:1: error: expected ‘)’ before ‘*’ token
prototypes.h:2: error: expected ‘)’ before ‘*’ token
prototypes.h:3: error: expected ‘)’ before ‘*’ token
prototypes.h:4: error: expected declaration specifiers or ‘...’ before ‘Node’

我是新来的,所以如果我搞砸了这个问题的格式或者已经回答了,我深表歉意。提前致谢。

最佳答案

您在源文件中定义类型 MainTrie(和 TrieNode),但在头文件(在函数原型(prototype)中)中包含对它们的引用。

解决方案:将 typedef/struct 声明移动到头文件。

关于c - 在 ')' token 和 '*' 的冲突类型之前预期为 'function',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26246397/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com