【问题标题】:"warning: 'struct matrix' declared inside parameter list [enabled by default]" and error: conflicting types for 'scanToken'“警告:在参数列表中声明的‘结构矩阵’[默认启用]”和错误:‘scanToken’的类型冲突
【发布时间】: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


【解决方案1】:

您必须在函数原型之外声明struct matrix,就像错误消息所暗示的那样。

您的scanner.h 标头中有:

struct token scanToken(struct matrix refTable);

由于在该标头中没有事先声明 struct matrix 或在读取之前读取标头,因此 struct matrix 是一种新的独特类型。它也不完整,因此您确实需要使用指向它的指针。

您可以像这样简单地修复它:

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

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

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

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

#ifndef SCANNER_H_INCLUDED
#define SCANNER_H_INCLUDED

…current contents…

#endif // SCANNER_H_INCLUDED

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

【讨论】:

    猜你喜欢
    • 2013-09-27
    • 2014-08-05
    • 1970-01-01
    • 2022-11-02
    • 1970-01-01
    • 2020-10-19
    • 2013-02-15
    • 2012-02-26
    相关资源
    最近更新 更多