“警告:在参数列表中声明了‘结构矩阵’[默认启用]";和错误:“scanToken"的类型冲突 [英] "warning: 'struct matrix' declared inside parameter list [enabled by default]" and error: conflicting types for 'scanToken'

查看:70
本文介绍了“警告:在参数列表中声明了‘结构矩阵’[默认启用]";和错误:“scanToken"的类型冲突的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在讨论这个问题,试图找出导致这些错误的原因,但到目前为止我一无所获.我有这个功能:

I've been pouring over this issue trying to figure out what is causing these errors, but so far I've come up with nothing. I have this function:

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

此代码当前在我的主函数中的此代码段中使用:

This code is currently used in this snippet in my main function:

 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文件(最后一张)中.但是,这会产生以下警告和错误.

the matrix struct is prototyped in another header file, which is included in the scanner.c file (pictured first), and the tokenize.c file (pictured last). However, this produces the following warnings and errors.

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

我一直在寻找相当长的时间,并尝试过多种方式重写事物,结果都是一样的.任何帮助将不胜感激.谢谢.

I've been searching for quite some time, and have tried rewriting things a number of ways, with the same result all around. Any help would be greatly appreciated. Thank you.

推荐答案

你必须在函数原型之外声明 struct matrix,就像错误信息暗示的那样.

You have to declare struct matrix outside a function prototype, like the error message implies.

您的 scanner.h 标头中有:

struct token scanToken(struct matrix refTable);

由于在该标头中没有预先声明 struct matrix,或者在读取前没有读取标头,所以 struct matrix 是一个新的不同类型.它也不完整,因此您确实需要使用指向它的指针.

Since there's no prior declaration of struct matrix in that header, or a header read before it is read, the struct matrix is a new distinct type. It's also incomplete, so you really need to use a pointer to it.

您可以简单地修复它:

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

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

To be able to pass the struct matrix by value instead of by pointer, you need a full definition of the structure, but pointers can be passed to incomplete types.

或者在 scanner.h 头文件中包含完整定义 struct matrix 的头文件.

Or include the header that defines the struct matrix fully in the scanner.h header.

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

Note that you should protect your headers with multiple inclusion guards:

#ifndef SCANNER_H_INCLUDED
#define SCANNER_H_INCLUDED

…current contents…

#endif // SCANNER_H_INCLUDED

您可能会在其中添加 #include "otherheader.h" — 另一个完整定义 struct matrix 的标题.

You might well add #include "otherheader.h" in that one — the other header that defines struct matrix in full.

这篇关于“警告:在参数列表中声明了‘结构矩阵’[默认启用]";和错误:“scanToken"的类型冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆