【发布时间】:2013-11-25 03:16:21
【问题描述】:
下面是我正在编写的 C 程序的第一部分。然而,gcc 在编译期间抛出错误,大意是 set、p 和 s1 都未声明,但它们已声明。我在这个文件中的每个函数都有这个问题,我不知道出了什么问题。我需要怎么做才能解决问题?
这个函数的具体错误:
a9.c: In function `makeaset':
a9.c:23: error: `set' undeclared (first use in this function)
a9.c:23: error: (Each undeclared identifier is reported only once
a9.c:23: error: for each function it appears in.)
a9.c:23: error: `p' undeclared (first use in this function)
a9.c:34: error: `s1' undeclared (first use in this function)
还有代码sn-p:
#include <stdio.h>
#include <stdlib.h>
#define SIZE 32
struct set {
char *data;
int count;
};
typedef struct set set;
void makeaset(set **s1)
{
int i;
set *p;
p = NULL;
p = malloc(sizeof(set));
if (p) {
p->data = malloc(SIZE);
if (p->data) {
for (i = 0; i < SIZE; i++)
p->data[i] = 0;
p->count = 0;
}
}
*s1 = p;
}
【问题讨论】:
-
您的类型的名称空间 (
struct set) 似乎与您的变量名称空间 (set) 发生冲突。为您的编译器和阅读您的代码的人推荐不同的名称。 -
正如@chux 所说,尝试
typedef struct set set_t;并将set *p;更改为set_t *p -
就像抽查一样:这段代码在 gcc 4.2.1 中编译得很好。你用的是什么版本?
-
3.4.3 是安装在我正在使用的服务器上的版本。一些更新会很好。 :) 谢谢你的信息!
-
您发布的代码 sn-p 是完全合法的。使用 gcc 4.7.2 和
gcc -std=c99 -pedantic -Wall -Wextra(也使用-std=c90和-std=c11)对我来说它编译时没有错误。此外,您的“sn-p”是 26 行,但您在第 34 行报告错误消息。请(a)复制并粘贴您自己的 sn-p 并编译它(我预测它也会为您编译而不会出错) 和 (b) 发布一个 exact 独立源文件,该文件显示错误。您接受的答案不正确。