"警告:' struct matrix'在参数列表中声明[默认启用]"和错误:' scanToken'的冲突类型

时间:2014-10-12 06:04:05

标签: c struct gcc-warning

我一直在倾向于试图找出导致这些错误的原因,但到目前为止我还没有提出任何问题。我有这个功能:

    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);

此代码目前在我的主要功能的代码段中使用:

 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);

我一直在寻找相当长的一段时间,并试图以多种方式重写事物,结果相同。任何帮助将不胜感激。谢谢。

1 个答案:

答案 0 :(得分:3)

您必须在函数原型之外声明struct matrix,如错误消息所示。

您有scanner.h标题:

struct token scanToken(struct matrix refTable);

由于该标头中没有事先声明struct matrix,或者在读取之前读取了标头,因此struct matrix是一种新的不同类型。它也是不完整的,所以你真的需要使用它的指针。

您可以将其修改为:

struct matrix;
struct token scanToken(struct matrix *refTable);

为了能够通过值而不是通过指针传递struct matrix,您需要完整的结构定义,但指针可以传递给不完整的类型。

或者在struct matrix标题中包含完全定义scanner.h的标题。

请注意,您应该使用多个包含保护来保护您的标头:

#ifndef SCANNER_H_INCLUDED
#define SCANNER_H_INCLUDED

…current contents…

#endif // SCANNER_H_INCLUDED

你可能会在那一个中​​添加#include "otherheader.h" - 另一个完全定义struct matrix的标题。

相关问题