如何解决"参数的类型不完整"错误?

时间:2015-03-31 14:55:01

标签: c arrays function struct

我是新手,我需要帮助来调试我的代码。 当我编译它时,形式参数1的类型是不完整的'形式参数2的类型不完整'

中出现错误
 printf("Age is %d years.\n", calc_age(birth, current));

虽然'参数1('出生')的类型不完整'和'参数2('当前')具有不完整的类型'错误出现在

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;
}

2 个答案:

答案 0 :(得分:4)

您的程序显示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);

答案 1 :(得分:1)

#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

之前定义结构