如何解决 "parameter has incomplete type" 错误?
How to resolve "parameter has incomplete type" error?
我是新手,我需要帮助来调试我的代码。
当我编译它时 'type of formal parameter 1 is incomplete' and type of formal parameter 2 is incomplete' 错误出现在
printf("Age is %d years.\n", calc_age(birth, current));
while 'parameter 1 ('birth') has incomplete type' and 'parameter 2 ('current') has incomplete type'错误出现在
int calc_age (struct date birth, struct date current) {
感谢帮助,谢谢!
#include <stdio.h>
int calc_age (struct date birth, struct date current);
int main(void)
{
struct date {
int month[1];
int day[1];
int year[1];
};
struct date birth, current;
char c;
printf("Input the birthdate (MM/DD/YY): ");
scanf("%d %c %d %c %d", birth.month, &c, birth.day, &c, birth.year);
printf("Input date to calculate (MM/DD/YY): ");
scanf("%d %c %d %c %d", current.month, &c, current.day, &c, current.year);
printf("Age is %d years.\n", calc_age(birth, current));
return 0;
}
int calc_age (struct date birth, struct date current) {
int age;
if (birth.month[0] < current.month[0]) {
age = (current.year[0] - birth.year[0]);
} else if (birth.month[0] > current.month[0]) {
age = (current.year[0] - birth.year[0] - 1);
} else {
if (birth.day[0] <= current.day[0]) {
age = (current.year[0] - birth.year[0]);
} else {
age = (current.year[0] - birth.year[0] - 1);
}
}
return age;
}
您的程序显示 incomplete type
错误,因为 struct date
的范围仅限于 main()
函数。在 main()
之外,结构定义不可见。
因此,struct date
定义应该在全局范围内,以便它在 calc_age()
中可见(也许还有其他函数)。更好的是,如果您可以为此目的创建和维护一个头文件。
也就是说,在您的代码中,根据您当前的要求,去掉结构中的单元素数组,例如
struct date {
int month;
int day;
int year;
};
还有 scanf()
声明
scanf("%d %c %d %c %d", birth.month, &c, birth.day, &c, birth.year);
应该阅读
scanf("%d %c %d %c %d", &birth.month, &c, &birth.day, &c, &birth.year);
#include <stdio.h>
struct date {
int month[1];
int day[1];
int year[1];
};
int calc_age (struct date birth, struct date current);
int main(void)
{
struct date birth, current;
char c;
printf("Input the birthdate (MM/DD/YY): ");
scanf("%d %c %d %c %d", birth.month, &c, birth.day, &c, birth.year);
printf("Input date to calculate (MM/DD/YY): ");
scanf("%d %c %d %c %d", current.month, &c, current.day, &c, current.year);
printf("Age is %d years.\n", calc_age(birth, current));
return 0;
}
int calc_age (struct date birth, struct date current) {
int age;
if (birth.month[0] < current.month[0]) {
age = (current.year[0] - birth.year[0]);
} else if (birth.month[0] > current.month[0]) {
age = (current.year[0] - birth.year[0] - 1);
} else {
if (birth.day[0] <= current.day[0]) {
age = (current.year[0] - birth.year[0]);
} else {
age = (current.year[0] - birth.year[0] - 1);
}
}
return age;
}
你应该在main
之前定义结构
我是新手,我需要帮助来调试我的代码。 当我编译它时 'type of formal parameter 1 is incomplete' and type of formal parameter 2 is incomplete' 错误出现在
printf("Age is %d years.\n", calc_age(birth, current));
while 'parameter 1 ('birth') has incomplete type' and 'parameter 2 ('current') has incomplete type'错误出现在
int calc_age (struct date birth, struct date current) {
感谢帮助,谢谢!
#include <stdio.h>
int calc_age (struct date birth, struct date current);
int main(void)
{
struct date {
int month[1];
int day[1];
int year[1];
};
struct date birth, current;
char c;
printf("Input the birthdate (MM/DD/YY): ");
scanf("%d %c %d %c %d", birth.month, &c, birth.day, &c, birth.year);
printf("Input date to calculate (MM/DD/YY): ");
scanf("%d %c %d %c %d", current.month, &c, current.day, &c, current.year);
printf("Age is %d years.\n", calc_age(birth, current));
return 0;
}
int calc_age (struct date birth, struct date current) {
int age;
if (birth.month[0] < current.month[0]) {
age = (current.year[0] - birth.year[0]);
} else if (birth.month[0] > current.month[0]) {
age = (current.year[0] - birth.year[0] - 1);
} else {
if (birth.day[0] <= current.day[0]) {
age = (current.year[0] - birth.year[0]);
} else {
age = (current.year[0] - birth.year[0] - 1);
}
}
return age;
}
您的程序显示 incomplete type
错误,因为 struct date
的范围仅限于 main()
函数。在 main()
之外,结构定义不可见。
因此,struct date
定义应该在全局范围内,以便它在 calc_age()
中可见(也许还有其他函数)。更好的是,如果您可以为此目的创建和维护一个头文件。
也就是说,在您的代码中,根据您当前的要求,去掉结构中的单元素数组,例如
struct date {
int month;
int day;
int year;
};
还有 scanf()
声明
scanf("%d %c %d %c %d", birth.month, &c, birth.day, &c, birth.year);
应该阅读
scanf("%d %c %d %c %d", &birth.month, &c, &birth.day, &c, &birth.year);
#include <stdio.h>
struct date {
int month[1];
int day[1];
int year[1];
};
int calc_age (struct date birth, struct date current);
int main(void)
{
struct date birth, current;
char c;
printf("Input the birthdate (MM/DD/YY): ");
scanf("%d %c %d %c %d", birth.month, &c, birth.day, &c, birth.year);
printf("Input date to calculate (MM/DD/YY): ");
scanf("%d %c %d %c %d", current.month, &c, current.day, &c, current.year);
printf("Age is %d years.\n", calc_age(birth, current));
return 0;
}
int calc_age (struct date birth, struct date current) {
int age;
if (birth.month[0] < current.month[0]) {
age = (current.year[0] - birth.year[0]);
} else if (birth.month[0] > current.month[0]) {
age = (current.year[0] - birth.year[0] - 1);
} else {
if (birth.day[0] <= current.day[0]) {
age = (current.year[0] - birth.year[0]);
} else {
age = (current.year[0] - birth.year[0] - 1);
}
}
return age;
}
你应该在main