【问题标题】:How to resolve "parameter has incomplete type" error?如何解决“参数类型不完整”错误?
【发布时间】:2015-03-31 14:55:00
【问题描述】:

我是新手,在调试代码方面需要帮助。 当我编译它'形式参数1的类型不完整'并且形式参数2的类型不完整'错误出现在

 printf("Age is %d years.\n", calc_age(birth, current));

虽然'参数1('birth')的类型不完整'和'参数2('current')的类型不完整'错误出现在

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

【问题讨论】:

  • scanf()返回成功读取的项数:测试scanf()的返回值!如果它在程序结束之前没有打印东西......请记住stdout是缓冲的。在printf("..."); 之后添加fflush(stdout);,内容将在正确的时间显示。您可能希望在打印内容时添加换行符:在打印的字符串末尾添加 \n。喜欢printf("...\n",...);
  • struct date 的定义移到calc_age 的原型之前。
  • int year[1]; 等于 int year;,如果不需要数组,请删除数组语法。

标签: c arrays function struct


【解决方案1】:

您的程序显示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 的数组作为字段是不寻常的。我不知道这样的功能在哪里有用。而函数calc_age() 会更优雅。
【解决方案2】:
#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之前定义结构

【讨论】:

    猜你喜欢
    • 2023-04-10
    • 1970-01-01
    • 2019-11-25
    • 1970-01-01
    • 2018-04-20
    • 2021-05-24
    • 2019-05-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多