【发布时间】: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 循环之后多了一个
}