【问题标题】:C program not compiling when using headers使用头文件时 C 程序未编译
【发布时间】:2012-10-11 12:15:58
【问题描述】:

(C PROGRAM) 我正在尝试编译一个使用标头的 main.c,但出现以下错误。 当我不使用标头(主文件中的所有方法)时,一切正常。

在字符串 S 中,程序查找所有出现的单词并返回出现次数最多的单词。

我正在编译使用:gcc main.c

谢谢。

错误

In file included from main.c:9:0:
frequence.h:4:16: warning: useless storage class specifier in empty declaration [enabled by default]
main.c: In function ‘main’:
main.c:15:10: error: variable ‘word’ has initializer but incomplete type
main.c:15:10: warning: passing argument 1 of ‘show_all_words’ from incompatible pointer type [enabled by default]
frequence.h:6:17: note: expected ‘char *’ but argument is of type ‘char (*)[34]’
main.c:15:10: error: invalid use of undefined type ‘struct stat_mot’
main.c:15:19: error: storage size of ‘word’ isn’t known

ma​​in.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "frequence.h"

#define LONGUEURMAX 4

int main(char *argv[]) {
  char texte[] = ";! one two, tree foor one two !:;";
  struct stat_mot word = show_all_words(&texte);

  printf("%s : %d\n", word.word, word.frequency);

  return (EXIT_SUCCESS);
};

frequence.h

#ifndef DEF_FREQUENCE
#define DEF_FREQUENCE

typedef struct stat_mot;
int count_word(char * , char * );
struct stat_mot show_all_words(char *);

#endif

frequence.c

#include "frequence.h"

typedef struct stat_mot {
    char * word;
    int frequency;
} stat_mot;

int count_word(char * mot, char * text) {
  int n = 0;
  char *p;

  p = strstr(text, mot);  
  while (p != NULL) {
    n++;
    p = strstr(p + 1, mot);
  }  

  return n;
}

stat_mot show_all_words(char * text) {
    char * text_rw = strdup(text);
    char * p = strtok(text_rw, " .,;:-!?");

    char word_to_return[strlen(text)];
    int  word_frequence = 0;

    while (p != NULL) {
      if (strlen(p) >= 0) {
        int offset = p - text;
        int count = count_word(p, text);

        if (word_frequence < count) {
          strcpy(word_to_return, p);          
          word_frequence = count;
        }
      };

      p = strtok(NULL, " .,;:-!?");
    }

    free(text_rw);
    struct stat_mot word = { word_to_return, word_frequence };
    return word;
}

【问题讨论】:

  • 另请注意,main() 在 C 中的有效声明是:int main(int argc, char *argv[])int main(void)。除非,您正在使用一些特定于编译器的功能,允许您声明不同类型的 main()
  • 顺便说一句,没有人关心你尝试了多少。这与问题无关。

标签: c linux gcc compiler-construction


【解决方案1】:

至少,您需要将stat_mot 的定义从frequence.c 移动到frequence.h。 main() 尝试创建它的实例,如果没有结构定义,您将无法这样做。

还有一些其他问题:

  • 我很难相信char*char(*)[34] 之间的类型不匹配可以通过将所有内容放在一个文件中来解决。所以我希望修复与您将代码组织到文件中无关,只是您应该传递texte,而不是&amp;texte
  • “空声明中的无用存储类说明符”- 是一个令人困惑的错误消息,但它的出现是因为在 C 语法中 typedef 是一个存储类说明符。不要问为什么。基本上,typedef struct stat_mot; 是错误的,但没关系,因为您可以在移动结构定义时将其删除。

【讨论】:

    【解决方案2】:

    你的结构定义必须在标题中。

    【讨论】:

      【解决方案3】:

      您必须将您的stat_mot 结构放入frequence.h

      另外,main() 应该声明为

      int main(int argc, char **argv)
      

      texte 应该是char *

      int main(char *argv[])
      {
          char *texte = ";! one two, tree foor one two !:;";
          struct stat_mot word = show_all_words(texte);
      

      您还应该在frequence.c 中包含&lt;string.h&gt;&lt;stdlib.h&gt;,因为您使用strstrstrlenfree

      frequence.c 中,strlen 始终大于或等于零,因此比较始终为真。

      【讨论】:

        【解决方案4】:

        标题仅适用于定义——例如编译确实有效,但链接器不知道在哪里寻找函数的主体。

        尝试用gcc main.c frequence.c编译

        另外,typedef struct stat_mot; 没有太多意义——struct stat_mot 目前没有定义,而且,你也没有用这个typedef 给它一个新名称。 我认为即使允许分离结构定义也没有意义 - 毕竟,如果你只是分发头文件,人们应该知道如何使用结构(例如,看看它包含什么)

        【讨论】:

          【解决方案5】:

          您在头文件中声明结构stat_mot(前向声明),但您在源文件frequence.c定义它。因此它不能从main.c 使用。

          【讨论】:

            【解决方案6】:

            只需将结构声明放入头文件即可:

            /* in frequence.h */
            struct stat_mot {
                char* word;
                int frequency;
            };
            

            所以两个翻译单元(即.c 文件)都可以看到它。

            【讨论】:

              【解决方案7】:

              struct stat_mot需要在头文件中定义或者使用指针。

              哦,而且(虽然这不是问题)

              typedef struct stat_mot;

              您需要像这样指定第二种类型名称:

              typedef struct stat_mot stat_mot;

              【讨论】:

                【解决方案8】:

                也许你应该写这个

                typedef struct stat_mot stat_mot;
                

                在你的 frequence.h 然后

                typedef struct stat_mot {
                    char * word;
                    int frequency;
                };
                

                在 .c 文件中。

                .h 文件只是数据文件或方法的声明,但你应该清楚地声明它。

                提示:不需要 ;在 main(){} 的末尾;

                【讨论】:

                  猜你喜欢
                  • 2011-08-10
                  • 2017-08-23
                  • 1970-01-01
                  • 1970-01-01
                  • 2022-06-13
                  • 2021-11-23
                  • 2015-06-20
                  • 2012-12-02
                  • 1970-01-01
                  相关资源
                  最近更新 更多