嘿伙计们我正在为一个项目工作,将文件中的单词添加到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’
我是新来的,所以如果我搞砸了这个问题的格式或者已经回答了我,我道歉。 提前谢谢。
答案 0 :(得分:2)
您在源文件中定义类型MainTrie
(和TrieNode
),但在头文件中包含对它们的引用(在函数原型中)。
解决方案:将typedef
/ struct
声明移至头文件。