【发布时间】:2014-10-12 06:04:05
【问题描述】:
我一直在努力解决这个问题,试图找出导致这些错误的原因,但到目前为止我一无所获。我有这个功能:
struct token scanToken(struct matrix refTable){
struct token send;
int counter = 0;
int currState = refTable.start;
while (1) {
printf("%d ", currState);
char c = getchar();
send.buffer[counter] = c;
int class = classifyChar(c);
char result = refTable.grid[class][currState].action;
currState = refTable.grid[class][currState].nextState;
if (currState = 99){
findEnd();
send.recrej = 'd';
return send;
}
else if (currState = refTable.accept){
if (c == EOF){
send.isEnd = 't';
}
else{
send.isEnd = 'f';
send.recrej = 'a';
}
}
++counter;
}
return send;
}
与以下头文件匹配:
struct token {
char buffer[512];
char recrej;
char isEnd;
};
void findEnd(void);
struct token scanToken(struct matrix refTable);
int classifyChar(char c);
这段代码目前在我的main函数中这个sn-p中使用:
struct matrix table;
table = buildMatrix(argv[1]);
char c;
int class;
struct token found;
found = scanToken(table);
while(found.isEnd != 't'){
if (found.recrej == 'a'){
printf("recognized '%s'\n", found.buffer);
}
else {
printf("rejected\n");
}
found = scanToken(table);
}
矩阵结构的原型在另一个头文件中,该文件包含在scanner.c 文件(上图)和tokenize.c 文件(最后图)中。但是,这会产生以下警告和错误。
In file included from scanner.c:10:0:
scanner.h:16:31: warning: 'struct matrix' declared inside parameter list [enabled by default]
struct token scanToken(struct matrix refTable);
^
scanner.h:16:31: warning: its scope is only this definition or declaration, which is probably not what you want [enabled by default]
scanner.c:60:14: error: conflicting types for 'scanToken'
struct token scanToken(struct matrix refTable){
^
In file included from scanner.c:10:0:
scanner.h:16:14: note: previous declaration of 'scanToken' was here
struct token scanToken(struct matrix refTable);
我已经搜索了很长一段时间,并尝试了多种方式重写,结果都是一样的。任何帮助将不胜感激。谢谢。
【问题讨论】:
-
struct matrix的定义周围是否有任何#ifdefs 或类似名称?看起来很像包含不起作用。 -
没有使用#ifdefs。不幸的是,这可能是一个简单的解决方法,但它不是导致它的原因。
-
感谢您的帮助。
-
创建一个新的 .c 文件,并将所有相关代码复制并粘贴到其中,包括您的头文件。如果它有效,那么#includes 有一些问题。
-
传递完整结构的行为,而不是简单的指向结构的指针,会导致链接器在“工作”区域中分配此类结构。然后将原始结构复制到工作区(并且可能,根据编写的代码,复制回其原始位置)。我建议 1) 在头文件中实际定义结构矩阵的格式 2) #include 该头文件。 3) 将两个参数传递给 scanmatrix 函数,一个用于当前结构,一个用于当前返回的结构。使用 void return。
标签: c struct gcc-warning