c - "warning: ' 结构矩阵 ' declared inside parameter list [enabled by default]"和错误 : conflicting types for 'scanToken'

标签 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);
        }

矩阵结构在另一个头文件中原型(prototype)化,该头文件包含在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);

我已经搜索了很长一段时间,并尝试了多种方式重写内容,但结果都是相同的。任何帮助将不胜感激。谢谢。

最佳答案

您必须在函数原型(prototype)之外声明结构矩阵,就像错误消息所暗示的那样。

您的 scanner.h header 中有:

struct token scanToken(struct matrix refTable);

由于在该 header 中没有事先声明结构矩阵,或者在读取之前没有读取 header ,因此结构矩阵是一种新的不同类型。它也是不完整的,因此您确实需要使用指向它的指针。

您可以简单地修复它:

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

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

或者在 scanner.h header 中包含完全定义结构矩阵的 header 。

请注意,您应该使用多个包含防护来保护 header :

#ifndef SCANNER_H_INCLUDED
#define SCANNER_H_INCLUDED

…current contents…

#endif // SCANNER_H_INCLUDED

您可能会在其中添加#include "otherheader.h" - 完整定义结构矩阵的另一个 header 。

关于c - "warning: ' 结构矩阵 ' declared inside parameter list [enabled by default]"和错误 : conflicting types for 'scanToken' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26322385/

相关文章:

c - 内核如何处理对共享映射的并发访问?

C MPI 矩阵乘法错误

时间:2019-03-08 标签:c++staticnon-static

c - 如何将 memcpy() 函数与动态分配结构一起使用

无法将结构的地址分配给类型为指向该结构的指针的 typedef 的变量

C 迭代到一个非常大的数字 - 关于 unsigned int 的编译器警告

c - Posix 消息队列接收/发送/打开不起作用?

arrays - 传递包含类或结构的数组

c++ - "Control reaches end on non-void function"with do { 返回结果; } 而(条件);

结构 : Can it be directional when passed to a function? 中的 Golang channel