C 请求非结构或联合中的成员
因此,我为qsort定义了函数compare,但以下错误显示:C 请求非结构或联合中的成员,c,function,struct,qsort,C,Function,Struct,Qsort,因此,我为qsort定义了函数compare,但以下错误显示: 1.c: In function ‘compare’: 1.c:235:7: error: request for member ‘count’ in something not a structure or union 1.c:235:17: error: request for member ‘count’ in something not a structure or union 1.c:237:12: error: requ
1.c: In function ‘compare’:
1.c:235:7: error: request for member ‘count’ in something not a structure or union
1.c:235:17: error: request for member ‘count’ in something not a structure or union
1.c:237:12: error: request for member ‘count’ in something not a structure or union
1.c:237:23: error: request for member ‘count’ in something not a structure or union
有人知道为什么吗?我的意思是,这不像是我拼错了名字:但是,你确实拼错了名字:(
您在
compare
函数中引用words
,并在其外部定义word
[编辑]你说
它被定义为一个全局结构。在哪里?你在这里复制的源没有可发现的单词定义
[/编辑]
由于您是从原始形式编辑文章的,您现在的问题是,rullof
已发布-您正在使用
访问
项目。问题是ia
和ib
是指向const struct word
的指针。要访问结构的成员,请使用指针我们使用箭头(->
)而不是点
另一件事是,当您声明ia
和ib
时,请确保以与上面声明相同的方式拼写结构名称
所以你的代码应该是:
struct word
{
char wordy[100];
int count;
};
int compare(const void* a, const void* b)
{
const struct word *ia = (const struct word *)a;
const struct word *ib = (const struct word *)b;
if(ia->count > ib->count)
return 1;
else if(ia->count == ib->count)
return 0;
else
return -1;
}
但是它在程序开始时被定义为一个全局结构,并且它必须是(由于其他函数).edit:lmao,名称是正确的,但仍然是相同的错误。嗯……它所在的程序有大约10个不同的函数,我在程序的开头写了这个结构,没有在任何一个函数中——在#include stdio.h等之后,这还不够吗?@deviance——仔细阅读Kevin所说的。他的答案是正确的,并指出这一点单词
(在比较功能中)没有在任何地方定义。虽然是全局定义的,但您已经定义了word
。只需在比较函数中将words
更改为word
。它并没有解决问题,修复后的错误是相同的。@越轨-在人们试图回答您的问题时编辑原始帖子会让人困惑,应该避免。谢谢!c挂起*ia.count到ia->count修复了它,现在,您已经编辑了原始帖子以修复拼写错误。将来,复制/粘贴真实的源代码-这不是您最好的回忆。建议还显示您的qsort()
调用。根据您想要排序的内容,比较函数可能会有其他问题。更改为(*ia).count
或ia->count
已生成答案是一个错误。
struct word
{
char wordy[100];
int count;
};
int compare(const void* a, const void* b)
{
const struct word *ia = (const struct word *)a;
const struct word *ib = (const struct word *)b;
if(ia->count > ib->count)
return 1;
else if(ia->count == ib->count)
return 0;
else
return -1;
}