【问题标题】:Warning: data definition has no type or storage class despite including header警告:尽管包含标头,但数据定义没有类型或存储类
【发布时间】:2018-10-04 08:57:30
【问题描述】:

我正在使用 GLib 数组。右边开头的代码包括:

#include <gmodule.h>

我有一个排序功能:

gint key_sort(gconstpointer a, gconstpointer b) {...}

还有一个使用数组的函数:

GArray * score_keysizes( ... )
{
   GArray * keys = g_array_new (FALSE, FALSE, sizeof(key_candidate));
   for (...)
   {
     ...      
     g_array_append_val(keys,temp);
     ...
   }
   g_array_sort(keys,key_sort);
}

但是这不会编译。

gcc -c -g -Wall -Wextra -mms-bitfields -IC:/msys64/mingw64/include/glib-2.0 -IC:/msys64/mingw64/lib/glib-2.0/include -IC:/msys64/mingw64/include -o Crypto.o Crypto.c
Crypto.c:345:3: warning: data definition has no type or storage class
 g_array_sort(keys,key_sort);
 ^~~~~~~~~~~~
Crypto.c:345:3: warning: type defaults to 'int' in declaration of 'g_array_sort' [-Wimplicit-int]
Crypto.c:345:3: warning: parameter names (without types) in function declaration
Crypto.c:345:3: error: conflicting types for 'g_array_sort'
In file included from C:/msys64/mingw64/include/glib-2.0/glib.h:31,
             from C:/msys64/mingw64/include/glib-2.0/gmodule.h:28,
             from Crypto.c:7:
C:/msys64/mingw64/include/glib-2.0/glib/garray.h:114:9: note: previous declaration of 'g_array_sort' was here
 void    g_array_sort              (GArray           *array,
         ^~~~~~~~~~~~
...

这使它看起来只有在假设原型之后才找到 g_array_sort 的原型。发生了什么?

MCVE 代码:

#include <stdio.h>
#include <stdlib.h>
#include <gmodule.h>

float hamdist_keyscore(const unsigned char *txtdat, unsigned int txtsize, unsigned int keysize);


typedef struct{
  unsigned int key;
  float score;
}key_candidate;

gint key_sort(gconstpointer a, gconstpointer b)
{
  if (((key_candidate *)a)->score > ((key_candidate *)(b))->score) return 1;
  else if (((key_candidate *)a)->score < ((key_candidate *)(b))->score) return -1;
  /* if (a.score == b.score)*/ return 0;  

}

GArray * score_keysizes(unsigned char* crypt, unsigned int len, unsigned int minkeysize, unsigned int maxkeysize)
{

  GArray * keys = g_array_new (FALSE, FALSE, sizeof(key_candidate));

  for (unsigned int i = minkeysize; i <= maxkeysize; i++)
  {
      key_candidate temp;
      temp.score = hamdist_keyscore(crypt, len, i);
      temp.key = i;
      g_array_append_val(keys,temp);
    }
  }

  g_array_sort(keys,key_sort);

  return keys;
}

编译时出现 MCVE 错误:

gcc -c -g -Wall -Wextra -mms-bitfields -IC:/msys64/mingw64/include/glib-2.0 -IC:/msys64/mingw64/lib/glib-2.0/include -IC:/msys64/mingw64/include -o Crypto.test.o Crypto.test.c
Crypto.test.c:35:3: warning: data definition has no type or storage class
   g_array_sort(keys,key_sort);
   ^~~~~~~~~~~~
Crypto.test.c:35:3: warning: type defaults to 'int' in declaration of 'g_array_sort' [-Wimplicit-int]
Crypto.test.c:35:3: warning: parameter names (without types) in function declaration
Crypto.test.c:35:3: error: conflicting types for 'g_array_sort'
In file included from C:/msys64/mingw64/include/glib-2.0/glib.h:31,
                 from C:/msys64/mingw64/include/glib-2.0/gmodule.h:28,
                 from Crypto.test.c:3:
C:/msys64/mingw64/include/glib-2.0/glib/garray.h:114:9: note: previous declaration of 'g_array_sort' was here
 void    g_array_sort              (GArray           *array,
         ^~~~~~~~~~~~
Crypto.test.c:37:2: error: expected identifier or '(' before 'return'
  return keys;
  ^~~~~~
Crypto.test.c:38:1: error: expected identifier or '(' before '}' token
 }
 ^
Crypto.test.c: In function 'score_keysizes':
Crypto.test.c:33:2: warning: control reaches end of non-void function [-Wreturn-type]
  }
  ^

【问题讨论】:

  • 你能提供一个非伪代码minimal reproducible example吗?显然有些问题,但没有告诉你无意中删除了所有这些... 的什么样的问题。 “填空”对诊断问题没有帮助。
  • @StoryTeller 添加了 MCVE 代码及其生成的错误。
  • 你在 for 循环之后多了一个}

标签: c gcc mingw glib


【解决方案1】:

问题在您的 MCVE 中变得明显。记下我添加的### cmets。

GArray * score_keysizes(unsigned char* crypt, unsigned int len, unsigned int minkeysize, unsigned int maxkeysize)
{ // ### 1

  GArray * keys = g_array_new (FALSE, FALSE, sizeof(key_candidate));

  for (unsigned int i = minkeysize; i <= maxkeysize; i++)
  { // ### 2
      key_candidate temp;
      temp.score = hamdist_keyscore(crypt, len, i);
      temp.key = i;
      g_array_append_val(keys,temp);
    } // ### 2
  } // ### 1

  g_array_sort(keys,key_sort);

  return keys;
}

它们匹配打开右大括号。所以g_array_sort被推送到文件作用域,并被当作函数声明。

【讨论】:

  • 谢谢。错过了这让我感到盲目。
  • @Gerhard - 有时我们都有隧道视觉。只是表示该休息一下了。
  • @Gerhard 请注意,您的错误的根本原因是您不一致的编码风格。如果你写了像if (((key_candidate *)a)-&gt;score &gt; ((key_candidate *)(b))-&gt;score) return 1; else if (((key_candidate *)a)-&gt;score &lt; ((key_candidate *)(b))-&gt;score) return -1; 这样的不可读的goo,就会发生不好的事情。因果报应是编程中的一件事,格式错误的代码总会回来困扰你。
  • @Lundin 是的,所有的选角都是令人眼花缭乱的。我昨天在一切开始工作后对其进行了重构。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-03-13
  • 1970-01-01
  • 2014-06-20
  • 2014-08-18
  • 1970-01-01
  • 2015-07-18
  • 1970-01-01
相关资源
最近更新 更多